Convert text into ascending order using string in C


This program sorts the characters in a string in ascending order using the Bubble Sort algorithm. The program first takes an input string from the user using the "fgets" function, which allows a user to input a string of up to 100 characters. The length of the string is then determined using the "strlen" function from the "string.h" library.

Next, the program sorts the characters in the string by repeatedly looping through the string and comparing adjacent characters. If a character on the left is larger than the character on the right, they are swapped. This process continues until the entire string is sorted in ascending order. Finally, the program outputs the sorted string to the user.

Source Code

#include <stdio.h>
#include <string.h>
int main()
{
  char str[100],ch;
  int i,j,l;
  printf("Input the string : ");
  fgets(str, sizeof str, stdin);
  l=strlen(str);
  /* sorting process */
  for(i=1;i<l;i++)
  {
    for(j=0;j<l-i;j++)
	{
	  if(str[j]>str[j+1])
	  {
	    ch=str[j];
	    str[j] = str[j+1];
	    str[j+1]=ch;
	  }
	}
  }
   printf("Result : \n");
   printf("%s\n\n",str);
   return 0;
}
To download raw file Click Here

Output

Input the string : TUTORJOES
Result : EJOORSTTU


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C