直接上代码
#include <stdio.h> #include <string.h> #include <conio.h> #include <assert.h> int delete_char(const char * str, const char * sub_str, char * result) { assert( str != NULL && sub_str != NULL); int i,j; int k = 0; int count = 0; char *p = result; for (i = 0 ; str[i] != '\0'; i++) { int temp = i; j = 0; while(str[i] != NULL && sub_str[j] != NULL && str[i] == sub_str[j]) { i++; j++; } if (sub_str[j] != '\0') { i = temp; p[k] = str[i]; k++; } else { count += 1; i--; } } p[k]='\0'; return count; } int main() { char a[] = "hello1234he123llo"; char b[] = "8"; char c[sizeof(a) - 1] = ""; printf("%d\n",delete_char(a, b, c)); printf("%s\n",c); return 0; }从一个字符串中删除一个子串后返回该字符串和包含的子串的个数
最新推荐文章于 2022-03-12 23:18:21 发布