I am one of those new assignment people, BUT I have tried to resolve this for a while now. Cant find much on google or other C++ sites.

I am trying to compare string lengths of 3 words and then display them in order of length.

My code is (I get stuck just before my first if statement):

#include <iostream>
#include <string.h>

using namespace std;

int main ()
int x,y,z;
string word1 = "word1 string";
string word2 = "word2 string";
string word3 = "word3 string";

cout<<"Please enter 3 words/n";

x = strcmp(word1, word2); ***THIS LINE IS THE PROBLEM***

if (strcmp (word1, word2)< 0)
wordA = word1
wordB = word2
if (strcmp (word1, word2) > 0)
wordA =word2
wordB =word1

y = strcmp (wordA, word3)
if (y < 0)
wordsml = wordA

z = strcmp (wordB, word3)
if (z < 0)
wordm = wordB

if (z > 0)
wordlge = word3

cout<<"The order of words from smallest to largest is "<<word sml<<" and "<<wordm<<" and "<<wordlge<,endl;

return 0


I know I have done something wrong in my setout, but the explanation given to me is not very clear to me and I dont know how to resolve : ******no matching function for call to 'strcmp(std::string&, std::string&)' ****** Can someone head me in the right direction?


10 Years
Discussion Span
Last Post by orko

strcmp takes C-style strings, not C++ string objects. The string class has overloaded the relational operators, so you can use < and > to compare them. But if you have your heart set on strcmp, you can do this:

x = strcmp(word1.c_str(), word2.c_str());

and u can use stricmp() instead of strcmp() to ignore upper case n lower case ..... actually this is not good way to compare string sizes....... bcoz:

int strcmp (const char str1[], const char str2[]);

compares str1 and str2 if they have same character.

int strlen (const char str[]);

function..... find both length seperately using this function, then compare.... good luck!

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.