Jump to content

  • Log In with Google      Sign In   
  • Create Account


Comparing strings


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 30 July 2012 - 05:30 AM

I have a non-terminated string of variable size, being read from a file and being copied into a buffer (see the following).

char szParam0[32];
b_file >> str;
strcpy(szParam0,str);

In this case szParam0=="player"

The problem that I am having is that when I try to compare with if() statements the result is never true.

if(szParam=="player") // Never equals true as szParam is a 32 byte char buffer.

Knowing that the string held in szParam can be variable length how can I use the if statement to compare against another string of text?

Any help would be greatly appreciated.

Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1148

Like
2Likes
Like

Posted 30 July 2012 - 05:57 AM

You can either use the strcmp function or use std::string, which also provides for string comparisons.

#3 rip-off   Moderators   -  Reputation: 7650

Like
4Likes
Like

Posted 30 July 2012 - 07:23 AM

I assume you mean "NUL terminated", not "non" terminated.

To be clear about why this doesn't work, you are comparing an array with a pointer to a literal string. In this circumstance, the array decays to a pointer to the first element. The literal string exists somewhere in the executable data, it will never occupy the same place as the array, so the comparison will always fail.

As always, trying to use raw character arrays as strings is hazardous for beginners. They make it too easy to do the wrong thing. Unless you have an exceptionally good reason, you should always use std::string for this.

In particular, reading unconstrained number of characters from an external source into a fixed length buffer is a security risk. A well crafted file could result in the execution of arbitrary code.

#4 arkane7   Members   -  Reputation: 213

Like
1Likes
Like

Posted 30 July 2012 - 09:20 AM

To add on what RulerOfNothing noted, there is a C standard function called strcmp

use it like this

if ( strcmp(str1, str2) == 0) do some stufff.....


it returns an integer, if it is zero then the strings are equivalent. From what i know it will return a negative number if the first string is less than the second (the first nonequivalent letter/character has an ASCII number less than the second strings) and will return positive if the first nonequivalent letter in the first has an ASCII number greater than the seconds

basically like this:
  • strcmp(s1, s2) < 0 /// s1 < s2
  • strcmp(s1, s2) > 0 //// s1> s2
  • strcmp(s1, s2) ==0 //// s1 == s2

the library for this function should be string.h or stdlib.h (possibly cctype.h) but im not really sure


and as stated by rip-off, it is much safer to use C++ strings instead. You dont need a special function, s1==s2 is defined for those strings, as is a multitude of varying features. just #include <string> (not string.h, the C-style one) and you're ready to go.
Always improve, never quit.

#5 BinaryPhysics   Members   -  Reputation: 294

Like
3Likes
Like

Posted 30 July 2012 - 09:24 AM

I have a non-terminated string [...]


'sz' is the Hungarian prefix for a zero-terminated string. Only use it if you indeed mean what rip-off has assumed.

The problem that I am having is that when I try to compare with if() statements the result is never true.


[source lang="cpp"]if(szParam == "player") /* compare the address of szParam to the location where "player" is stored */[/source]

String literals are actually constant pointers. It isn't possible to compare them this way because you're trying to compare two pointers and not the data they point to.

The easiest method would be to use strncmp in the Standard C library. It obeys the rules that arkane7 stated but allows you to specify a maximum number of characters to compare.

[source lang="cpp"]if(strncmp(szParam, "player", sizeof("player")) == 0){ /* the two strings are equal */}[/source]

Remember that str{n}cmp is a character-by-character check of two strings. If one string is longer than the other you'll flow over the end of the smaller string.

Edited by BinaryPhysics, 30 July 2012 - 12:26 PM.


#6 chukangore   Members   -  Reputation: 105

Like
1Likes
Like

Posted 30 July 2012 - 09:51 AM

The literal string exists somewhere in the executable data, it will never occupy the same place as the array, so the comparison will always fail.Posted Image

#7 !Null   Members   -  Reputation: 380

Like
1Likes
Like

Posted 30 July 2012 - 01:30 PM

I'm not sure if this has anything to do with what you're looking for because im not amazing with C++

but in my text game i have something like

cin >> response;

if(response.compare("yes"))
{
do stuff
}
else
{
do other stuff
}
/********************************************************************************\
/**********************He Who Dares, Wins**********************************\
/********************************************************************************\

#8 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 30 July 2012 - 05:06 PM

Thanks guys, you are all awesome, +1 to each of you :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS