Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the sample code for string substitution in C language

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you how to write sample code about string substitution in C language. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Replace, which means replacing all str2 in str1 with another string str3. The replacement process and the search process can be combined. In the above loop search process, every time a str2 is found, it is replaced with a str3, and the pointer p is moved after the replacement. There are several situations of replacement: one is that str2 and str3 are the same length, one is that str3 is longer than str2, and the other is that str3 is shorter than str2. The first case is relatively simple, just use the strncpy function, and in the latter two cases, you need to move the elements in the str1. For example, in the above example, str2= "the", suppose str3 = "this", str3 is longer than str2, in order to have enough space, every time you find a the, you have to move back 1 byte from the beginning of the character after the to the end of'\ 0', that is, 4 bytes are freed for this (3 bytes of the plus 1 byte moved). Suppose str3 = "ok", str3 is shorter than str2, to fill the gap, every time you find a the, you have to move forward one byte from the beginning of the character after the the to the'\ 0' at the end, which is enough to leave two bytes for the ok. After moving, use the strncpy function to copy the str3 to where the str2 is located. In the following program, str_replace is used to implement the replacement function.

Key points:

Note the difference between a character array and a string; adding'\ 0' after the last character of the character array forms a string.

/ *-function: implement the replacement description of the string: enter the original string on the first line and the string to be replaced on the second line Enter a new string input example on the third line: There is an orange, do you want to eat it?orangeapple output example: There is an apple, do you want to eat it?Author: Zhang KaizhouDate: 2019-8-9 11:11:32---*/#include # define MAXSIZE 300void str_replace (char * str1 Char * str2, char * str3) Int main () {char str1 [MAXSIZE]; char str2 [MAXSIZE]; char str3 [MAXSIZE]; gets (str1); gets (str2); gets (str3); str_replace (str1, str2, str3); puts (str1); return 0;} void str_replace (char * str1, char * str2, char * str3) {int I, j, k, done, count = 0, gap = 0; char temp [MAXSIZE]; for (I = 0) I < strlen (str1); I + = gap) {if (str1 [I] = = str2 [0]) {done = 0; for (j = I, k = 0; k < strlen (str2); if (str1 [j]! = str2 [k]) {done = 1; gap = k; break }} if (done = = 0) {/ / the string to be replaced has been found and replaced with for (j = I + strlen (str2), k = 0; j < strlen (str1); jobless, str1 +) {/ / save the remaining characters in the original string temp [k] = str1 [j] } temp [k] ='\ 0characters]; / / change the character array into a string for (j = I, k = 0; k < strlen (str3); jcharacters, str3 +) {/ / string substitution str1 [j] = str3 [k]; count++ } for (k = 0; k < strlen (temp); jobless, kink +) {/ / the remaining string str1 [j] = temp [k];} str1 [j] ='\ 0characters; / / turn the character array into the string gap = strlen (str2) }} else {gap = 1;}} if (count = = 0) {printf ("Can't find the replaced string!\ n");} return;} above is how to write the sample code for string substitution in C language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report