Solved

Another mind-bending problem...comparing character strings.

Posted on 2002-04-20
16
197 Views
Last Modified: 2010-04-02
I'm trying to input two character strings and, if they are equal, or true, print a statement.  Otherwise- end the program.  Heres the code.

#include <iostream>
using namespace std;

int main()
{
     char stringone[256];
     char stringtwo[256];

     cout << "Enter string one: ";
     cin.get(stringone,256);
     cout << "stringone is: " << stringone << endl;

     cin.ignore(256,'\n');

     cout << "Enter string two: ";
     cin.get(stringtwo,256);
     cout << "String two is: " << stringtwo << endl;
     
     if (stringone==stringtwo)
          cout << "This is the back door";



     return 0;
}

This code acts like these two strings are not true regardless of input.  I dont know if I'm properly comparing them.
Any help- much appreciated.
Rixx

0
Comment
Question by:rixxagain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +3
16 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 6957297
Here's where things start to get fun (i.e., more complex).

The value of a string reference without a subscript, e.g., 'stringone', is the memory address of that string.

To do the string comparison you want to do, use the 'strcmp' function.  'strcmp(s1,s2)' returns a 0 if the srings are the same, a -1 if s1 is 'less' than s2 (i.e., would appear before s2 in sort order), or a +1 if s1 is greater than s2.

So, the test you want to do would be:

if (0==strcmp(stringone,stringtwo))

Note that 'strcmp' is case sensitive.  To do a case insensitive compare, use 'stricmp'.

0
 
LVL 4

Expert Comment

by:ct.smith
ID: 6957316
A minor nitpick to cookre's comment, it's strcmp does not always return -1, 0, or +1, it returns some value less than 0 if s1 is less than s2, zero if they are equal or some value larger than zero if s1 is greater than s2.  You can't count on particular values aside from the zero.
0
 
LVL 22

Expert Comment

by:cookre
ID: 6957324
One stands corrected.
(<i>hanging head in shame</i>)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6957537
You could of course use the "string" class, which will give you the kind of intuitive "feel" you are looking for i.e. comaprison (==) as well as a lot of other nice features. Of course it does have some overhead (speed and size) but it sure is easy to use.
0
 

Author Comment

by:rixxagain
ID: 6957718
Thank you gentlemen.  I'll try and get to this today. Tomorrow certainly.  Questions that I do have.
   Strcmp is case sensitive to the contents of the strings?  Like YES does not equal yes?
   Also- what are string classes?  This book that I have really isnt helping me or teaching me much.
Thx again
Rixx
0
 

Author Comment

by:rixxagain
ID: 6957848
Is there a way to compare an input character string with a predefined character string?  Like input stringone, and compare it to a stringtwo already defined in the program?
0
 
LVL 22

Expert Comment

by:cookre
ID: 6957990
That is correct, using strcmp(), "YES" is not equal to "yes" or "YEs" or, yEs", or, well you get the idea.  Using stricmp(), they ARE all equal.


To compare against a fixed string, you can either:

if (0==stricmp(stringone,"wee willie wumpus"))

or:

strcpy(stringtwo,"was a wyly ole wit"); // )strcpy is string copy)
...
if (0==stricmp(stringone,stringtwo))

The latter method has the advantage of letting you use 'stringtwo' numerous places, but, should you want to change the value of it, you need only change it in one place.

As to those string classes, they belong to the much touted 'Object Oriented Programming' (spoken in reverential tones).  There are many arguments for and against the early introduction of such things.  My opinion is that they are best left until later, but to avoid turning the thread into something else, I'll just leave it at that.
0
 
LVL 2

Accepted Solution

by:
LoungeLizard earned 150 total points
ID: 6958413
The string class is a class defined in standard C++. Here's a little example of how you use it

#include <string>
#include <iostream>

using namespace std;

void main( )
{
  string s1 = "test1";
  string s2 = "test2";
  string s3 = s1 + " AND " + s2;
  string s4 = "test1 AND test2";
  cout << "The string s1 = " << s1 << endl;
  cout << "The string s2 = " << s2 << endl;
  cout << "The string s3 = " << s3 << endl;
   
  if(s3 == s4)
    cout << "s3 is equal to s4" << endl;
  else
    cout << "s3 is not equal to s4" << endl;    
   
  system("pause");
}
0
 
LVL 22

Expert Comment

by:cookre
ID: 6958512
Rixx, go with LL on that.
0
 

Expert Comment

by:ballan00
ID: 6961207
If you really like C strings, try this:

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

int main()
{
     char* str1 = "Hello";
     char* str2 = "hello";

     if (strcmp(_strlwr(str1), _strlwr(str2)) == 0)
     {
          cout << "Match!!!" << endl;
     }
     else
     {
          cout << "Don't Match!!!" << endl;
     }

     return 0;
}
0
 

Expert Comment

by:ballan00
ID: 6961246
Dummy Me -

My C/C++ is a bit rusty.  The above although it will compare the strings correctly, will also convert them to all lower case.  I am used to toLower functions in other languages that just return a temporary string, and don't actually modify the original.
0
 

Expert Comment

by:manas_ghosal
ID: 6968257
stringone and stringtwo are actually the starting memory addresses of two different 256 continuous location which are allocated when you declare within your main function the two statements:
                 char stringone[256];
                 char stringtwo[256];
So since the starting addresses(stringone and stringtwo) of two different array are different so stringone==stringtwo would never be true. To compare the two string you must compare each character of the two string or you can use "strcmp" function, which does the same thing.
best of luck
0
 

Author Comment

by:rixxagain
ID: 6970138
OK Cookre.  I'll go ahead with LL, and thanks for all your input as well.  You, and all, have been most helpfull. I dont want to hold up this thread any longer, and Im certain that I'll be asking more.  I'm learnin alot more about C++ from this site than that damm book.

Thanks again to all
Rixx
0
 

Author Comment

by:rixxagain
ID: 6970143
Works like a charm LL.  Its exactly what I wanted to do.  I have to say- I'm really impressed at how well everyone on this site knows C++.
Take care and thanks again.
Rixx
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6970155
Always a pleasure. Have fun!
0
 
LVL 22

Expert Comment

by:cookre
ID: 6970226
It's always pleasant to see one's beliefs borne out - books and manuals can be great reference material, but you can really learn only by doing.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question