Problem Description
How to replace first occurrence of a character with another character in a string using loop in C programming.
Input:
1. The String character
2. The Character that needs to be replaced in the given string
3. The Character to be replaced in the stringCODING ARENA
#include <stdio.h>
#include <string.h>
int main()
{
char str[100],o,n;
int i;
scanf("%s",str);
scanf("%s",&o);
scanf("%s",&n);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==o)
{
str[i]=n;
break;
}
}
printf("%s",str);
return 0;
}
Test Case 1
Input (stdin)srmunaversitylearningcentre a i
Expected Outputsrmuniversitylearningcentre
Test Case 2
Input (stdin)SRASRASRA A M
Expected OutputSRMSRASRA
No comments:
Post a Comment