Enter two strings, find out how many second substrings are in the first string, the number of outputs, and output the string after deleting all the second substrings.
Input: ABCDCDE CD
Output: 2 Abe
#include <iostream> #include <string>using namespace Std;int delete_sub_str (const char *str,const char *sub_ Str,char *result) {const char *p,*q;char *t,*tmp;int N=0;int num=0;//The number of identical substrings P=str;q=sub_str;t=result;n=strlen (q); tmp = New Char[n+1];memset (tmp,0,n+1); while (*p) { memcpy (tmp,p,n);//copy n characters from the initial position of P to Tmpif (strcmp (tmp,q) ==0)//tmp=q {Num++;memset (tmp,0,n+1);p =p+n;} else{ *t = *p;p++;t++;memset (tmp,0,n+1);}} Delete Tmp;return num;} void Main () {char s[100] = {'};int ' num = delete_sub_str ("123abc123de1234fg1hi34j123k", "123", s); Cout<<num<<endl;cout<<s<<endl;}
Huawei Machine Test-delete the same substring