CT 03
#include <stdio.h>
#include <string.h>
#define MAX 1000 // Enough to hold the result
int main() {
char s1[MAX] = "I scream, you scream, we all scream for ice cream!";
char s2[] = "scream";
char s3[] = "shout";
char result[MAX] = ""; // Resulting string
char *pos = s1; // Current position in s1
char *match; // Pointer to next occurrence
int count = 0;
while ((match = strstr(pos, s2)) != NULL) {
// Copy part before match
strncat(result, pos, match - pos);
// Add replacement word
strcat(result, s3);
// Move past the match
pos = match + strlen(s2);
count++;
}
// Add the remaining part after the last match
strcat(result, pos);
printf("Resulting string: %s\n", result);
printf("Number of replacements: %d\n", count);
return 0;
}
Comments
Post a Comment