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!

Recommended Answers

All 8 Replies

http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html

strcmp:

Compares the C string str1 to the C string str2.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached.

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;
}
commented: a) you should have posting this first then, and b) it's about time you learnt about code tags -4

The link I posted should solve your problem.

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.

#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";
	}

}

thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.