Hi,
I tried comparing the strings as follows but it doens't work. Pls tell me the correct way, thnx a lot:
Code:char string = "yyy";
if ( string == "yyy" )
printf( "match" );
else
printf( "doesn't match" );
Printable View
Hi,
I tried comparing the strings as follows but it doens't work. Pls tell me the correct way, thnx a lot:
Code:char string = "yyy";
if ( string == "yyy" )
printf( "match" );
else
printf( "doesn't match" );
you cant use the equality operator on strings it doesnt work, use this method
#include<string.h>
char[32] string1;
char[32] string2;
if(strcmp(string1, string2)==NULL)
{
cout<<"they are equal";
}
so i can't do it just by ordinary code or do it by scratch is that what you mean?
thnx
I could be wrong, but K & R says strcmp returns 0 when they're equal.
k thnx
yep sorry,my mistake
that part of the code should have been
!=NULL
:o
I think you actually want:
if(strcmp(string1, string2)==0)
{
cout<<"they are equal";
}
>>so i can't do it just by ordinary code or do it by scratch is that what you mean?
Yes you can.
First the strings must be the same length ( strlen() or lstrlen() ) and then you will have to loop thru each string testing each index individually.
As you can see using strcmp() is easier.Code:if(lstrlen(sString1)!=lstrlen(sString2))
return FALSE;//no match
for(i=0;i<lstrlen(sString1);i++)
{
if(sString1[i]!=sString2[i])
return FALSE;//no match so exit
}
return TRUE;//got this far must be match
Yes, you could either use strcmp or test how novacain said to. The reason being is when you say:
These are char arrays and when you don't use the brackets to specify an element, then you start comparing addresses of the arrays. Of course, unless they are at the same address in mem, it will always turn out, "no".Code:if (string1 == string2)