Problem Statement: Sort String Values in Ascending Order You are required to wri
ID: 3610543 • Letter: P
Question
Problem Statement: Sort String Values in Ascending Order You are required to write a program that accepts 5 input strings from the user. After sorting strings into ascending order the string will be displayed. Detailed Description: Declare a character type string array. Use for loop Get five string values from the user Sort them in ascending order Display them after sorting Ascending order means string values starting from 'a' will come first, and then starting from 'b' and so on. Sample Output enter string l : Muhammad enter string 2 : Abdullah enter string 3 : Usman enter string 4 : Ali enter string 5 : Noureen Sorted string Abdullah Ali Muhammad NoureenExplanation / 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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.