Search This Blog

Translate

write c program to print string in descending and ascending order of their alphabet

write c program to print string in descending and ascending order of their alphabet

#include
#include
void main()
{
  int i,j,l,str[20],temp;
  char s1[20];
  clrscr();
  printf("\n Enter string:");
  gets(s1);
   for(i=0;s1[i]!='\0';i++)
   {   j=s1[i];    //assign ascii to int var&then int array
      str[i]=j;  }
   str[i]='\0';

   for(i=0;str[i]!='\0';i++)
   {
    for(j=i+1;str[j]!='\0';j++)
    {
      if(str[i]
      {
      temp=str[i];
      str[i]=str[j];      //sorting of ascii
      str[j]=temp;
      }
    }
  }
 for(i=0;str[i]!='\0';i++)
     s1[i]=str[i];
  printf("\n String in descending order:");
    printf("%s",s1);
  printf("\n String in ascending order:");
  l=strlen(s1);
  for(j=l-1;j>=0;j--)
    printf("%c",s1[j]);
  getch();
}



C Program example List

Google+ Badge