C program that prompts the user for an input string, sorts its characters, and prints out the sorted output
Posted by Samath
Last Updated: May 04, 2016

Write a complete program that prompts the user for an input string, sorts its characters, and prints out the sorted output. Assume the string contains no spaces and is at most 30 characters. Sort the characters according to byte values, irrespective of the symbols those values represent, from smallest to largest. The output should be one contiguous string, printed on one line. Example: “Input: apple” should print “aelpp”.

Solution:

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
	char str[30],temp;
	int i,j,n;
	printf("Enter your string: ");
	scanf("%s", &str);
	n = strlen(str);
	for (i = 0; i < n;i++)
		for (j = i + 1; j < n; j++){
			if (str[i] > str[j]){
				temp = str[i];
				str[i] = str[j];
				str[j] = temp;
			}
	}
	printf("%s\n", str);
	return 0;

}