Description
To sort string in alphabetical order, sort characters of strings.
C/C++
/* C Program to sort characters of strings in alphabetical order*/ //Save it as SortStringCharacter.c #include<stdio.h> #include<string.h> int main(){ char str[50], temp; int i,j; printf("Enter a string : "); scanf("%s",str); //Calculating length of string int len = strlen(str); //Swapping the characters string if previous is greater later for(i=0;i<(len-1);i++){ for(j=(i+1);j<len;j++){ if(str[i]>str[j]){ temp = str[i]; str[i] = str[j]; str[j] = temp; } } } printf("The characters of string after sorting : %s",str); return 0; }
Input: Enter a string : meera Output: aeemr
Java
/* Java Program to sort characters of strings in alphabetical order*/ //Save it as SortStringCharacter.java import java.io.*; import java.util.Arrays; import java.util.Scanner; public class SortStringCharacter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str; System.out.println("Enter a string : "); str = scanner.nextLine(); //Converting string into characters of array char[] charVal = str.toCharArray(); //Sorting the character array Arrays.sort(charVal); //Printing after coverting character array into string System.out.println(String.valueOf(charVal)); } }
Input: Enter a string : meera Output: aeemr
Related Programs
1) Program to Sort set of strings in alphabetical order2) Program to Convert Lowercase to Uppercase of a String
3) Program to Append one String to another String
4) Program to Compare Two Strings
5) Program to Reverse a String
6) Find Substring of a Given String
7) Program to Insert a String into Another String
8) Program to check String is Palindrome
9) Program to check entered character is vowel or consonants
10) Program to remove vowels from a String
11) Program to count uppercase and lowercase alphabet characters in String