Solved

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

Posted on 2002-04-20
16
192 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ array loading from DB inheriting previous data when empty 5 59
Modify static variables within a class in VC++ 3 95
cb: unreferenced local variable 11 72
C++ Language error 28 197
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

863 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

21 Experts available now in Live!

Get 1:1 Help Now