1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 //字符串中删除某一个字符 6 void deletech(char *str,char ch) 7 { 8 if (str == NULL) 9 { 10 return; 11 } 12 13 char *pstr = str; 14 while (*str != '\0') 15 { 16 if (*str != ch) 17 { 18 *pstr++ = *str++; 19 } 20 else 21 { 22 str++; 23 } 24 } 25 *pstr = 0; 26 } 27 28 //字符串中删除某一个字符串 29 void deletestr(char *str, char *des) 30 { 31 char *find = strstr(str, des); 32 while (find != NULL) 33 { 34 int len = strlen(des); 35 36 //从find开始到结尾的长度 37 for (int i = 0; i <= strlen(find) - len; i++) 38 { 39 //替换,加长度则下标到该字符串的后一位 40 *(find + i) = *(find + len + i); 41 } 42 43 // 44 find = strstr(find + 1, des); 45 } 46 } 47 48 void main() 49 { 50 char str[100] = "i love you i love her i love china i love my family"; 51 52 printf("%s\n", str); 53 //deletech(str,'i'); 54 deletestr(str, "love"); 55 printf("%s\n", str); 56 system("pause"); 57 }