Program to sort a string in alphabetical order | faceprep

Program to sort a string in alphabetical order is discussed here. Given a string, the task is to sort the string in alphabetical order and display it as output.

For example, consider the following strings

Input: faceprep

Output: accefppr

Input: focus

Output: cfosu

Solution to this problem can be provided in two different ways.

Method 1: Sorting the string alphabetically by swapping the characters of the string.

Method 2: Sorting the string alphabetically using standard library functions.

Program to sort a string in alphabetical order by swapping the characters in the string is given beolw.

/* C Program to sort a string in alphabetical order */
#include <stdio.h>
#include <string.h>

int main ()
{
char string[100];
scanf(“%s”,string);
char temp;

int i, j;
int n = strlen(string);


for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}

printf(“The sorted string is %s”, string);
return 0;
}

/* C++ Program to sort a string in alphabetical order */
#include <iostream>
#include <string.h>
using namespace std;

int main ()
{
char string[100];
cin >> string;
char temp;

int i, j;
int n = strlen(string);


for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}

cout << “The sorted string is ” << string;
return 0;
}

// Java program to sort a string in alphabetical order

import java.util.*;

public class Main
{

public static void main(String[] args)
{

String str;
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
int j = 0;
char temp = 0;

char[] chars = str.toCharArray();

for (int i = 0; i < chars.length; i++) {

for (j = 0; j < chars.length; j++) {

if (chars[j] > chars[i]) {
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}

}

}

for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}

}
}

# Python program to sort a string in alphabetical order
string = input()
j = 0
str1 = “”
temp = 0
str = list(string)

for i in range(0, len(str)):
for j in range(0, len(str)):
if(str[j] > str[i]):
temp = str[i]
str[i] = str[j]
str[j] = temp
print(str1.join(str))

Scan the characters one by one from the input string. If the second character is smaller than the first character, swap them and continue the same procedure until all the characters in the string are encountered.

Program to sort a string in alphabetical order using standard library functions is given below.

/* C++ Program to sort a string in alphabetical order */
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{

string s;
cin >> s;
sort(s.begin(), s.end());
for (auto c : s)
cout << c;

return 0;
}

// Java program to sort a string in alphabetical order

import java.util.*;

public class Main
{
public static String sort_string(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}

public static void main(String[] args)
{
String str;
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
String out_str = sort_string(str);

System.out.println(“Input String : ” + str);
System.out.println(“Output String : ” + out_str);
}
}

# Python program to sort a string in alphabetical order
str = input()
print(“Sorted string :”,”.join(sorted(str)))

Output:

sort a string in alphabetical order