C program to read a string and rewrite it in the alphabetical order


/* C program that will read a string and rewrite it in the alphabetical order. i.e. the word STRING should be written as GINRST. */


#include"stdio.h"
#include"string.h"

void main()
{
char str[20], k;
int i, j;

printf("Enter a string: \n");
scanf("%[^\n]", str);
for(i=0; str[i] != '\0'; i++)
{
for(j=i+1; str[j] != '\0'; j++)
{
if(str[i] > str[j])
{
k= str[i];
str[i] = str[j];
str[j] = k;

}
}
}
printf("%s", str);
printf("\n");

}

Output:



Comments

Popular Posts