Replace All Occurrences Of A Substring In A String With Another Substring | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

after watching for multiple times, i now understood it, hats off to your contribution

stellaroasis
Автор

another way to count the occurences of your substring is this:
although it uses another temporary char array.

char str[]="the cat chased the mouse"
char sub[] = "the"
char temp[strlen(sub)];
strcpy(temp, str);

int occurence=0;
while( strstr(temp, sub) != NULL)
{
occurence++;
strcpy( temp, strstr(temp, sub) + strlen(sub) );
}

//char original string (str) is being copied to temp array, then the temp array is being checked if it contains the substring, if it does it increments occurence and then overwrites the temp array EXCLUDING the found substring and repeats.

drig
Автор

Can this be done with a dynamic array and user-entered strings? Im a c++ student just started recently Edit: Professor wants us to use c-string

grunger