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.
#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
Input the string : TUTORJOES Result : EJOORSTTU
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions