12) Write a program that replaces the occurrence of a given character (say c) in a
primary string (say PS) with another string (say s).
Input
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)
Output
Print the string PS with every occurence of c replaced by s.
NOTE:- There are no whitespaces in PS or s.
Maximum length of PS is 100. Maximum length of s is 10.
#include
#include
#define MAX_LENGTH 105
void replace_string(char ps[], char c, char s[]) {
int i, j, k, len = strlen(ps);
char result[MAX_LENGTH];
int s_len = strlen(s);
for (i = 0, j = 0; i < len; i++) {
if (ps[i] == c) {
for (k = 0; k < s_len; k++) {
result[j++] = s[k];
}
} else {
result[j++] = ps[i];
}
}
result[j] = '\0';
strcpy(ps, result);
}
int main() {
char ps[MAX_LENGTH], c, s[MAX_LENGTH];
scanf("%s", ps);
scanf(" %c", &c);
scanf("%s", s);
replace_string(ps, c, s);
printf("%s\n", ps);
return 0;
}