c 字符串查找,替换

 1 char * strReplace(char *str, char *oldstr, char * newstr)
 2 {
 3     char *beg = strFind(str, oldstr);
 4     if (!beg)
 5         return str;
 6     int lenStr = strlen(str);
 7     int lenOldstr = strlen(oldstr);
 8     int lenNewstr = strlen(newstr);
 9     memmove(beg + lenNewstr, beg + lenOldstr, lenStr - (beg - str) - lenOldstr + 1);
10     memcpy(beg, newstr, lenNewstr);
11     return str;
12 }
13 
14 char * strFind(char * str, char * sub)
15 {
16     char * psub = sub;
17     char * pstr = str;
18     while (*str){
19         psub = sub;
20         pstr = str;
21         while (*str == *psub && *psub)
22         {
23             ++str;
24             ++psub;
25         }
26         if (!*psub)
27             return pstr;
28         else
29             str = pstr + 1;
30     }
31     return NULL;
32 }

 

转载于:https://www.cnblogs.com/endenvor/p/8119187.html

你可能感兴趣的:(c 字符串查找,替换)