IN C Read a list of strings into a vector Swap the smallest
IN C++
Read a list of strings into a vector
Swap the smallest value alphabetically into the first position
Print list
INPUT:
Monday
Tuesday
Wednesday
Thursday
Friday
OUTPUT:
Friday
Tuesday
Wednesday
Thursday
Monday
Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<string> input;
int totalStr;
string instr;
cout << \"Enter number of strings to add: \";
cin >> totalStr;
cout << \"Enter all the strings\" << endl;
for(int i = 0; i < totalStr; i++){
cin >> instr;
input.push_back( instr );
}
int minAt = 0;
for(int i = 1; i < totalStr; i++){
bool ans = lexicographical_compare( input[i].begin(), input[i].end(),
input[minAt].begin(), input[minAt].end() );
if( ans ){
minAt = i;
}
}
string temp = input[0];
input[0] = input[minAt];
input[minAt] = temp;
cout << \"Output: \" << endl;
for(int i =0 ; i < totalStr; i++){
cout << input[i] << endl;
}
return 0;
}
