Tuesday, 5 April 2016

C++ Program - Sort String words in Alphabetical Order Problem Solution.


Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    vector<string>words;
    string str;
    int n;

    cout << "Enter How Many Words: ";
    cin >> n;

    for(int i = 0; i < n; i++){
        cin >> str;
        words.push_back(str);
    }
    for(int i = 0; i < words.size(); i++){
        for(int j = 0; j < words.size()-1; j++){
            if(words[j] > words[j+1]){
                string tmp = words[j];
                words[j] = words[j+1];
                words[j+1] = tmp;
            }
        }
    }
    for(int i = 0; i < words.size(); i++)cout << words[i] << endl;
    return 0;
}

No comments:

Post a Comment