Question:- Write a program that replaces the occurence 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.
Test Case-
1) Input:
abcxy
b
gh
Output:- aghxy
2) Input:
Al@bal#20owL
l
LL
Output:- ALL@baLL#20owL
This is the code I wrote:
#include<stdio.h>
#include<string.h>
int main()
{
char PS[105];
char Final[105];
char ch;
int i;
fgets(PS, sizeof(PS), stdin);
scanf("%c",&ch);
scanf("%s",Final);
for(i=0;i<strlen(PS);i++)
{
if(PS[i]==ch)
{
for(i=0;i<strlen(Final);i++)
printf("%c",Final[i]);
}
else
printf("%c",PS[i]);
}
return 0;
}