Sunday, September 23, 2018

Remove Repeated Characters in a String

  • Problem Description

    Write a C program to remove all repeated characters in a string
  • CODING ARENA
  • #include <stdio.h>
    #include <string.h>
     
    int main()
    {
      char str[100];
      int i, j, k;

      scanf("%s",str);
      printf("%s\n",str);
     
      for(i = 0; i < strlen(str); i++)
      {
      for(j = i + 1; str[j] != '\0'; j++)
      {
      if(str[j] == str[i])  
    {
      for(k = j; str[k] != '\0'; k++)
    {
    str[k] = str[k + 1];
    }
      }
    }
    }
    printf("%s", str);
      return 0;
    }
  • Test Case 1

    Input (stdin)
    srmuniversity
    
    
    Expected Output
    srmuniversity
    
    srmunivety
  • Test Case 2

    Input (stdin)
    madam
    
    
    Expected Output
    madam
    
    mad

No comments:

Post a Comment