Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2002-04-20
16
Medium Priority
?
200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

647 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