Friday, August 17, 2018

REPLACE LAST OCCURENCE


  • Write a C program to replace last occurrence of a character with another in a given string

    The Input is as follows:
    1. The string input
    2. The character to be replaced in the input string
    3. The NEW Character to be replaced in the input string
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      char str[100],c,n;
      int i,index;
      index=-1;
      scanf("%s%s",str,&c);
      getchar();
      scanf("%s",&n);
      for(i=0;i!=' ';i++)
      {
        if(str[i]==c)
        {
          index=i;
        }
      }
      if(index!=-1)
      {
        str[index]=n;
      }
      printf("%s",str);
        

    return 0;
    }
  • Test Case 1

    Input (stdin)
    srasrasra
    
    a
    
    m
    
    
    Expected Output
    srasrasrm
  • Test Case 2

    Input (stdin)
    University
    
    i
    
    s
    
    
    Expected Output
    Universsty

No comments:

Post a Comment