C Code for deleting repeated characters in a sentence

C Program to delete duplicate characters in a string/sentence
//C code for removing repeated characters of a string/sentence

#include<stdio.h>

#include<string.h>

int main()

{

char a[200];

int i,j,k,n;


printf("Enter a few chars and strings:\n");

gets(a);

printf("\nEntered string is :%s \n",a);


printf("\nUpdated string without repeated characters is\n");

n=strlen(a);

for(i=0;i<n;i++)

{

   for(j=i+1;j<n;)//mind it no icrement
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
{

             a[k]=a[k+1];
}

          n--;//else array will contain 0 in deleted duplicate value
      }
      else
         j++;
   }
}


for(i=0;i<n;i++)

    printf("%c",a[i]);
printf("\n");

}

/*

Enter a few chars and strings:

india is great aa bb cc dd ee ff gg hh ii jj kk


Entered string is :india is great aa bb cc dd ee ff gg hh ii jj kk


Updated string without repeated characters is


inda sgretbcfhjk

*/

No comments:

Post a Comment