1,105,391 Community Members

how to know if 2 strings are identical

Member Avatar
SpyrosMet
Junior Poster in Training
54 posts since Feb 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey everyone.
I have a problem with 2 strings i need to compare. My code is the following

if((*(fixedparkpnt)).name == pname)
{
 //do stuff
}

I am trying to make a search proceedure based on a name given by the user but the condition is always false. I have tried the function strcmp(); but it compares only the numerical values of the strings so i dismissed it. Any suggestions to make the comparisson work?

Member Avatar
GuitarComet
Light Poster
41 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

In the code that you show you're comparing the addresses of two pointers, and not the actual contents of them. It's strange that 'strcmp' didn't work for you, since it's supposed to tell you when two strings are equal. One of the mistakes I did when I was new to C was to think that 'strcmp' returned 1 if the strings were equal, while it actually returns 0 if two strings are equal. Might that be the problem?

Member Avatar
Rahul.menon
Light Poster
28 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

use strcmp function

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

Two c-strings are identical if, the starting addresses are the same or if different(starting addresses) the characters that constitute the c-string are the same.

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
1
 
if((strcmp(string1, string2))==0) 
  string1 is equal to string2

or

int num = strcmp(string1, string2);
if(num==0) 
  strings are equal
else if(num < 1)
  string2 is greater than string1 //note the direction of 
   //the < (big "mouth", facing string2)
else
  string1 is greater than string 2 //string1 now has the "big mouth" of the > symbol
Member Avatar
abhimanipal
Master Poster
746 posts since Dec 2009
Reputation Points: 91 [?]
Q&As Helped to Solve: 104 [?]
Skill Endorsements: 0 [?]
 
1
 

If you want to make your own string comparison function

1. If length of string 1 is not equal to string 2 then, strings are not identical
2. for i:0, to strlen, check if the str1 == str2. If they are not equal then break out of the loop

Member Avatar
Agnusmaximus
Newbie Poster
10 posts since Nov 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-1
 

What do you mean by identical? If you mean the string content is identical
(e.g "hello world" = "hello world") then use strcmp.

strcmp evaluates two strings and if identical returns a 0.

In you code, I believe your comparing the value of pointers, and so if the pointers aren't identical, the "if" statement will always return false.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: