Thursday, September 13, 2018

REMOVE LAST OCCURRENCE


  • Problem Description

    Write a C program to read any string from user and remove the last occurrence of a given character from the string.
  • CODING ARENA
  • #include <stdio.h>
    #include <string.h>
    int main()
    {
      char str[100];
      char ch;
      int i,index,len;
      index=-1;
      scanf("%c %s",&ch,str);
      len=strlen(str);
      for(i=0;i<len;i++)
      {
        if(str[i]==ch)
        {
          index=i;
        }
      }
      if(index!=-1)
      {
        i=index;
        while(i<len)
        {
          str[i]=str[i+1];
          i++;
        }
      }
        printf("%s",str);
      
      return 0;
    }
  • Test Case 1

    Input (stdin)
    a
    
    madam
    
    
    Expected Output
    madm
  • Test Case 2

    Input (stdin)
    s
    
    msdhoni
    
    
    Expected Output
    mdhoni

No comments:

Post a Comment