Sign in to follow this  

Comparing input strings.

This topic is 4857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I compare input strings? I tried: [CODE] char string1[]; char string2[]; cin >> string1; cin >> string2; if(string1 == string2) { cout << "They are equal."; } else { cout << "They are not equal."; } [/CODE] What am I doing wrong?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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 :]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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()').

Share this post


Link to post
Share on other sites
Once you are more experienced with C++ you can use the std::string class as Alpha_ProgDes said. Its useful when you need to compare, add and subtract strings and really just want a string data type instead of having to work with char pointers.

Share this post


Link to post
Share on other sites

This topic is 4857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this