Monday, 12 March 2018

C program to rearrange the unique characters in a string in descending order

The following program reads a string as an input, displays the unique letters in the string in descending order.
For example, if the input is
innovation
the output of the program will be
vtonia

//Program to sort unique characters in a string

# include <stdio.h>

int main()
{
    char s[100],d[100],temp;
    int i=0,n,j=0,k;
    printf("Enter string\n");
    scanf("%s",s);
    while (s[i++]!='\0');
    n=i;
    if (n>=3 && n<100)
   {
       for(i=0;i<n;i++)
      {
          for(k=0;k<=j;k++)
         {
             if (s[i]==d[k])
             break;
         }
        if (k>j)
           d[j++]=s[i];
      }
       d[j]='\0';
  

      for(i=0;i<j-1;i++)
      for(k=i+1;k<j;k++)
       {
            if (d[i]<d[k])
            {
                temp=d[i];
                d[i]=d[k];
                d[k]=temp;
            }
       }
       printf("String after sorting\n");
       printf("-----------------------\n");
       printf("%s",d);
  }  
    return 0;
}


OUTPUT
Enter string                                                                                                                         
innovation   
                                                                                                                        
String after sorting                                                                                                                 
-----------------------                                                                                                              
vtonia