Solved

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

Posted on 2002-04-20
16
191 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
  • 5
  • 4
  • 3
  • +3
16 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
Comment Utility
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
Comment Utility
One stands corrected.
(<i>hanging head in shame</i>)
0
 
LVL 2

Expert Comment

by:LoungeLizard
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:cookre
Comment Utility
Rixx, go with LL on that.
0
 

Expert Comment

by:ballan00
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Always a pleasure. Have fun!
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now