Problem Statement:Sort String Values in Ascending Order You are required to writ
ID: 3610333 • Letter: P
Question
Problem Statement:Sort String Values in Ascending Order
You are required to writea program that accepts 5 input strings from the user. After sortingstrings into ascending order the string will bedisplayed.
DetailedDescription:
1. Declare acharacter type string array.
2. Use forloop
3. Get five stringvalues from the user
4. Sort them inascending order
5. Display them aftersorting
6. Ascending ordermeans string values starting from ‘a’ will come first,and then starting from ‘b’ and so on.
SampleOutput
enterstring1 : Muhammad
enterstring2 : Abdullah
enterstring3 : Usman
enterstring4 : Ali
enterstring5 : Noureen
Sorted string
Abdullah
Ali
Muhammad
Noureen
Usman
Explanation / Answer
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.