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" );
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";
}
Monday - what a way to spend a seventh of your life
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.
-Govtcheez
[email protected]
yep sorry,my mistake
that part of the code should have been
!=NULL
Monday - what a way to spend a seventh of your life
I think you actually want:
if(strcmp(string1, string2)==0)
{
cout<<"they are equal";
}
-Govtcheez
[email protected]
>>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
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
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)
1978 Silver Anniversary Corvette