0

Hello everyone I am trying to do this assignment in the book: Write a program that reads four words (as character strings) and display them in increasing and decreasing alphabetical sequence.
I have done the 1st (easy) part but not sure how to go about displaying words in alphabetical order.
Here is what I’ve got so far:

int main()
{
string word1;
string word2;
string word3;
string word4;
string stringEval;


cout << "Enter four words using spaces between them: " << endl;
cin >> word1 >> word2 >> word3 >> word4;

Thank you for your help!

Edited by happygeek: fixed formatting

5
Contributors
8
Replies
11
Views
8 Years
Discussion Span
Last Post by grisha83
-1

I asked for help not for giving me a hard time
but here is what i've got so far

// File:
// (description)
//
// Input:
// Output
//--------------------------------
// Class: CS210 Instructor: Ravi Gandham
// Assignment:  Date Assigned:
// Programmer:  Date Submitted:


#include<iostream>
#include <string>
using namespace std;


int main()
{
string word1;
string word2;
string word3;
string word4;
string stringEval;


cout << "Enter four words using spaces between them: " << endl;
cin >> word1 >> word2 >> word3 >> word4;


if (word1 > word2){
cout << "....." << word1;}
else
cout << "....." << word2;


if (word3 > word4){
cout << "....." << word3;}
else cout << "...." << word4;


/*stringEval.find
if ((word1 == 'A') && (word1 == 'a'))
*/


system("pause");
return 0;
}

Edited by happygeek: fixed formatting

Votes + Comments
a) you should have posting this first then, and b) it's about time you learnt about code tags
0

You can use compare since you are using strings rather than C-strings. Or strcmp could work, as the last poster mentioned, though you'd have to convert from strings to C-Strings.

0
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>

using namespace std;
bool compare(string s1,string s2)
{
	if(strcmp(s1.c_str(),s2.c_str())>0)
		return true;
	else
		return false;
}
int main()
{
string a[]={"abc","dafa","eeee","what"};
	vector<string> names(a,a+4);

// increasing alphabetical  sequence
sort(names.begin(),names.end());
	
	for(vector<string>::const_iterator itr=names.begin();itr<names.end();itr++)
	{
		cout<<*itr<<" \t";
	}
	cout<<endl;

// decreasing alphabetical sequence
	sort(names.begin(),names.end(),compare);
	for(vector<string>::const_iterator itr=names.begin();itr<names.end();itr++)
	{
		cout<<*itr<<"\t";
	}

}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.