Home > Back-end >  In the substring in a string of all locations
In the substring in a string of all locations

Time:10-26

Write a function to find substring in a string of all position and return to the main function of output
Such as the main series of abcaabc
Substring ABC then return 1 5
Which bosses can, c + + white

CodePudding user response:

Using STRSTR function

One example of C:
 # include & lt; stdio.h> 
# include & lt; String. H>

Void foundPos (char * dest, const char * subs, char * buf)
{
Char * PCH.
Int pos.
Char tmpStr [10].

PCH=STRSTR (dest, subs);
While (PCH!=NULL) {
Pos=PCH - dest;
Sprintf (tmpStr, "% d", pos + 1);
Strcat (buf, tmpStr);
Strcat (buf, "");
PCH=STRSTR (PCH + 1, subs);
}
}

Int main ()
{
Char STR []="abcbbbabc";
PCH char [100]={0};
Printf (" & the for 'ABC' in the \ "% s \ " n ", STR);
FoundPos (STR, "ABC", PCH);
Printf (" % s \ r \ n ", PCH);
return 0;
}
  • Related