Comparing input strings.

Started by
9 comments, last by Rebooted 19 years, 7 months ago
How would I compare input strings? I tried: char string1[]; char string2[]; cin >> string1; cin >> string2; if(string1 == string2) { cout << "They are equal."; } else { cout << "They are not equal."; } What am I doing wrong?
Advertisement
you should be using int strcmp(string1, string2). That function returns 0 if they are equal. What you are testing for is if both string occupy the same memory...

if(!strcmp(string1, string2))    // the strings are the same


hope I've helped. BTW, might need to include string.h unless some other include has already defined this for you

dwiel
Since strings in C are character arrays, the string variable isn't really a variable, but a location in memory. Hence a==b compares the memory locations.

strcmp(a,b) compares the actual strings [but watch out, it doesn't return what you expect]
try this:
#include <stdio.h>#include <string.h>char cBuffer1[128],cBuffer2[128];int main(void) {scanf("%s",cBuffer1);scanf("%s",cBuffer2);if ( !strcmp(cBuffer1,cBuffer2) ) {   printf("They are Equal\n");}else {   printf("They are Not Equal\n");}return 1;}


It's a classic rookie mistake to assume you can compare strings :) You cannot compare entire arrays, you have to compare each character column by column or use the strcmp function (wich is false if both strings are equal, hence the "!" )

You should also note strcmp() only works on null-terminated strings...
Imperio59 - C++ Programmer
Quote:Original post by Telastyn
[but watch out, it doesn't return what you expect]


What do you mean?
He means strcmp() will return 0 (or false) if both strings are the same...

Think of it this way: strcmp() returns the number of differences between the strings. If there are 0 differences, they are the same :)
Imperio59 - C++ Programmer
Thanks for the help :) Code works great.
Yeah, sorry for being brief, I kinda wanted you to look up the function somewhere more offical than the forums as it's kind of tricky and has some limitations. Others have covered better than I could anyways :]
are you using C or C++?

i asked because i see you were using cin and cout instead of scanf and printf. if you're using strings, why not just use #include <string>
#include <iostream>#include <string>using namespace std;int main () {  string word1 = "Name";  string word2 = "Place";  if (word1 == word2)     cout << "Gotta match!" << endl;  else     cout << "Different words." << endl;  return 0;} 

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Imperio59
He means strcmp() will return 0 (or false) if both strings are the same...

Think of it this way: strcmp() returns the number of differences between the strings. If there are 0 differences, they are the same :)


Not quite; it returns a negative number if the first is 'greater' (comes after in lexicographical order), and positive if the second is greater. Or else it's the other way around; but the point is that the sign of the result, if non-zero, tells which comes first. That's so it can be used for sorting (e.g. in C library 'qsort()').

This topic is closed to new replies.

Advertisement