REPLACE 1ST OCCURRENCE OF STRING

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 string Test Case 1 Input (stdin) srmunaversitylearningcentre a i Expected Output srmuniversitylearningcentre Test Case 2 Input (stdin) SRASRASRA A M Expected Output SRMSRASRA

Program :

  • #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;
    }