0% found this document useful (0 votes)
94 views3 pages

Program To Sort A String in Alphabetical Orde SDSDR - FACE Prep

The document discusses how to write programs to sort a string in alphabetical order using two different methods - by swapping characters of the string and using standard library functions. It provides code samples in C, C++ and Java to implement both methods and outputs for sample inputs.

Uploaded by

Sam Gonçalves
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views3 pages

Program To Sort A String in Alphabetical Orde SDSDR - FACE Prep

The document discusses how to write programs to sort a string in alphabetical order using two different methods - by swapping characters of the string and using standard library functions. It provides code samples in C, C++ and Java to implement both methods and outputs for sample inputs.

Uploaded by

Sam Gonçalves
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

13/04/2021 Program to sort a string in alphabetical order | FACE Prep

Login (https://www.faceprep.in/login/?rurl=/c/program-to-sort-a-string-in-alphabetical-order/)
Link copied to clipboard.
Wall of Love Share away! Dismiss
Explore
(https://www.faceprep.in/) Sign Up (https://www.faceprep.in/signup/?rurl=/c/program-to-sort-a-string-in-alphabetical-
(https://www.faceprep.in/reviews/)
order/)

Program to sort a string in


alphabetical order | FACE Prep
Published on 11 Mar 2020

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: face

Output: acef

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

C C++ Java Python 3


Explore 'C'
1 /* C Program to sort a string in alphabetical order */ Articles
2 #include <stdio.h>
(https://www.faceprep.in/c#articles)
3 #include <string.h>
4 Practice Exercises
5 int main () (https://www.faceprep.in/c#practice)
6 {
7 char string[100];
8 printf("\n\t Enter the string : ");
9 scanf(“%s”,string);
10 char temp;
11 int i, j;
12 int n = strlen(string);
13 for (i = 0; i < n-1; i++) {
14 for (j = i+1; j < n; j++) {
15 if (string[i] > string[j]) {
16 temp = string[i];
17 t i [i] t i [j]

Output

Enter the string : face


The sorted string is : acef

https://www.faceprep.in/c/program-to-sort-a-string-in-alphabetical-order/ 1/3
13/04/2021 Program to sort a string in alphabetical order | FACE Prep
Scan the characters one by one from the input string. If the second character
Login is smaller
(https://www.faceprep.in/login/?rurl=/c/program-to-sort-a-string-in-alphabetical-order/)
Link copied to clipboard.
Wall of Love Share away! Dismiss
Explore
than the first character,(https://www.faceprep.in/)
swap them and continue the same procedure until all the
Sign Up (https://www.faceprep.in/signup/?rurl=/c/program-to-sort-a-string-in-alphabetical-
(https://www.faceprep.in/reviews/)
characters in the string are encountered. order/)

Program to sort a string in alphabetical order using


standard library functions

C++ Java Python 3

1 /* C++ Program to sort a string in alphabetical order */


2 #include <iostream>
3 #include <algorithm>
4 #include <string>
5
6 using namespace std;
7
8 int main()
9 {
10
11 string s;
12 cin >> s;
13 sort(s.begin(), s.end());
14 for (auto c : s)
15 cout << c;
16 return 0;
17 }

Output

Enter the string : face


The sorted string is : acef

Recommended Programs

Basic string operations (https://www.faceprep.in/string-operations-in-c-c-java-and-


python/)

Frequency of characters in a string (https://www.faceprep.in/program-to-find-the-


frequency-of-characters-in-a-string/)

Non-repeating characters in a string (https://www.faceprep.in/find-the-first-non-


repeating-character-in-a-string/)

String is a palindrome or not (https://www.faceprep.in/check-whether-a-string-is-


palindrome-or-not/)

C (Https://Www.Faceprep.In/C)

C++ Programming (Https://Www.Faceprep.In/C-Plus-Plus)

Java Programming (Https://Www.Faceprep.In/Java)

Python Programming (Https://Www.Faceprep.In/Python)

Interview Preparation (Https://Www.Faceprep.In/Interview-Preparation)

   

If you have any feedback about this article and want to improve this, please write
to [email protected]

https://www.faceprep.in/c/program-to-sort-a-string-in-alphabetical-order/ 2/3
13/04/2021 Program to sort a string in alphabetical order | FACE Prep
Login (https://www.faceprep.in/login/?rurl=/c/program-to-sort-a-string-in-alphabetical-order/)
Link copied to clipboard.
Wall of Love Share away! Dismiss
Explore
(https://www.faceprep.in/) Sign Up (https://www.faceprep.in/signup/?rurl=/c/program-to-sort-a-string-in-alphabetical-
(https://www.faceprep.in/reviews/)
order/)

(https://www.faceprep.in/)

COMPANIES PROGRAD FACE PREP SOCIAL


TCS (https://www.faceprep.in/tcs/) Cognizant
About ProGrad Our Story Youtube
(https://www.faceprep.in/cognizant)
(https://www.faceprep.in/prograd/)(https://www.focusacademy.in/ourstory/)(https://www.youtube.com/ch
AMCAT IBM (https://www.faceprep.in/ibm/)
ProGrad Microdegree Contact Us Facebook (https://www.faceb
oDJMA)
(https://www.faceprep.in/amcat- (https://prograd.org/microdegree/) (https://www.faceprep.in/contact/)
WIPRO BYJU's ProGrad Bootcamp Terms & Conditions WhatsApp (https://www.focu
exam/)
(https://www.faceprep.in/wipro/) (https://www.faceprep.in/byjus/)
(https://prograd.org/bootcamp/) (https://www.faceprep.in/terms-and-
Infosys Deloitte Privacy Policy Instagram (https://www.insta
conditions/)
(https://www.faceprep.in/infosys/) (https://www.faceprep.in/deloitte/) (https://www.faceprep.in/privacy-policy/)
TATA ELXSI eLitmus Telegram (https://t.me/facepr
(https://www.faceprep.in/tata-elxsi/) (https://www.faceprep.in/elitmus- PLACEMENT PREP
Capgemini TCS Ninja Linkedin (https://www.linked
exam/)
(https://www.faceprep.in/capgemini/)(https://www.faceprep.in/tcs/) Articles
Accenture Cocubes
(https://www.faceprep.in/articles/)
(https://www.faceprep.in/accenture/) (https://www.faceprep.in/cocubes- Tests (https://www.faceprep.in/tests/)
Tech Mahindra Mindtree
exam/)
(https://www.faceprep.in/tech- (https://www.faceprep.in/mindtree/) Videos
mahindra/) (https://www.faceprep.in/videos/)
SUBJECTS Webinars
(https://www.faceprep.in/webinars/)
Quantitative Aptitude Python
(https://www.faceprep.in/quantitative-(https://www.faceprep.in/python/)
Java (https://www.faceprep.in/java/) C (https://www.faceprep.in/c/)
aptitude/)
Data Structures Algorithms
(https://www.faceprep.in/data- (https://www.faceprep.in/algorithms/)
Verbal Ability Logical Reasoning
structures/)
(https://www.faceprep.in/verbal- (https://www.faceprep.in/logical-
ability/) reasoning/)

© Focus 4D Career Education Pvt. Ltd. All rights reserved

https://www.faceprep.in/c/program-to-sort-a-string-in-alphabetical-order/ 3/3

You might also like