Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string equality

Status
Not open for further replies.

garishnikov

Programmer
Mar 20, 2003
6
AU

so..i dont know how to test if two strings are equal. need something equivalent to ".equals()" in java, just in c.

this is the code:

char c[2];

do
{
//some code

scanf("%s", c);

if(c == "y")
{ cont = 1; } else { cont = 0; }

}
while(cont)

Thanks
 
[tt]if ( strcmp( c, "y" ) == 0 )[/tt]

Though if you're after just one letter, this is much quicker
[tt]if ( c[0] == 'y' )[/tt]

In your example, you can remove the if() altogether, and just go with assigning a boolean result
[tt]cont = strcmp( c, "y" ) == 0;[/tt]

--
 
BTW, if you wish to do a case-insensitive string comparision
you can use the POSIX strcasecmp() or strncasecmp()
functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top