Problem Statement: Sort String Values in AscendingOrder You are required to writ
ID: 3610580 • Letter: P
Question
Problem Statement: Sort String Values in AscendingOrder
You are required to write a program that accepts 5 inputstrings from the user. After sorting strings into ascending orderthe string will be displayed.
Detailed Description:
1. Declare a character typestring 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
//i Think this is solution toyour problem. what you think#include<iostream> #include<conio.h> #include<string> using namespace std;
void swap(string*, int ,int ); void sort(string*);
int main(){ string strings[5]; for(int i=0;i<5;i++)//taking userinput { cout<<"Enter String"<<i+1<<":"; cin>>strings[i]; } sort(strings);//calling sort function for(int i=0;i<5;i++){//print names cout<<strings[i]<<endl; } cout<<endl;
} ///==================================// void swap(string *str, int i, int j) { string temp; temp = str[i]; str[i] = str[j]; str[j] = temp; } void sort (string *str) { for(int i=0;i<5;i++) { for(intj=i+1;j<5;j++) { if(str[i].compare(str[j]) >0) { swap(str, i, j); } } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.