ProblemStatement: Sort String Values in AscendingOrder You are required towrite
ID: 3610585 • Letter: P
Question
ProblemStatement: Sort String Values in AscendingOrder
You are required towrite a program that accepts 5 input strings from the user. Aftersorting strings into ascending order the string will bedisplayed.
DetailedDescription:
1. Declare acharacter type string array.
2. Use for loop
3. Get five string valuesfrom the user
4. Sort them in ascendingorder
5. Display them aftersorting
6. Ascending order meansstring values starting from ‘a’ will come first, andthen starting from ‘b’ and so on.
Sample Output
enter string1 : Muhammad
enter string2 : Abdullah
enter string3 : Usman
enter string4 : Ali
enter string5 : Noureen
Sorted string
Abdullah
Ali
Muhammad
Noureen
Usman
Explanation / Answer
#include<iostream>
using namespacestd;
int main(){
string str[5],s;
int i,j;
for(i=0;i<5;i++)
{
cout<<"Enter " <<" string"<<(i+1)<<" ";
cin>>str[i];
}
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
{
if(str[i].compare(str[j]) >0)
{
s = str[i];
str[i] = str[j];
str[j] = s;
}
}
for(i=0;i<5;i++)
cout<<str[i]<<endl;
cout<<endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.