To take a string from the User & then arrange the characters in that string in ascending order like if user enters "babcac" the program should print it as "aabbcc"

Showing Answers 1 - 6 of 6 Answers

baseersd

  • Jul 19th, 2007
 

Here is the code for sorting a string in ascending order or alphabetical order

#include<stdio.h>
int main()
{
    char str[20]="";
    char temp;
    int i,j;
    printf("nEnter the stringn");
    gets(str);

    for(i=0;i<strlen(str);i++)
    {
      temp=str[i];                        
     for(j=0;j<i;j++)
     {
                  if(str[j]>str[i])
                  {
                   temp=str[j];
                   str[j]=str[i];
                   str[i]=temp;                
                  }   
     }                          
    }   
    printf("nString after sorting in alphabetical order::");
    printf("%s",str);
getch();
return 0;
}

Explanation:
1)Accept the string using gets or scanf();
2)using bubble sort technique sort the string.
3)Print it

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions