How does it work? I want to write a function for it for myself.
I have the string "$$$$" , without quotes needed to stop a program from looping, but how do I convert the $$$$ to a number\value so I could type
while (END!=$$$$) blah blah
How does it work? I want to write a function for it for myself.
I have the string "$$$$" , without quotes needed to stop a program from looping, but how do I convert the $$$$ to a number\value so I could type
while (END!=$$$$) blah blah
It basically just loops through each character and sees if they're equal. If every character is equal, then the strings are equal.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
So is this an assignment for school where you are not allowed to use the string.h header? Just curious.
~/
Code://if str1 is bigger return a positive number, //if str2 is bigger return a negative number, //equal reutrn 0 int strcmp(char *str1,char *str2) { while(1) { if(*str1==*str2) { if(*str1=='\0')return 0; str1++; str2++; continue; } else { return *str1-*str2; } } } main() { printf("%d",strcmp("abc","abd")); // print -1 getch(); }
what does the * in (*str1==*str2) do?
and no we cant use strcmp cause well, we didn't reach there yet.
str1 is a pointer, its value is a memory address,
*str1 is a char which str1 point to.
I don't understand your second sentence,sorry.
Hey,
I always wondered this why does the strcmp int
<string.h>
return 0 if they compare equally so you cant do this
while(strcmp(str1,str2))
this would run if they were not equal Just wondering why the person who wrote that macro did that did he do it for any particular reason
> Just wondering why the person who wrote that macro did that did he do it for any particular reason
Upon completion, strcmp() returns an integer greater than, equal to or less than 0, if the string pointed to by s1 is greater than, equal to or less than the string pointed to by s2 respectively.
sometimes you need to know which string is greater and which is less
for strcmp, that is right, it returns 0 if they are equal. This may seem really messed up, but it actually helps out a LOT
One way I found that strcmp is REALLY helpful is in a sorting algorithm. Say you have a list of strings and you need them alphabetically, right? well, right away you'd think, ok, do a loop through 'em all and use some algorithm to place them in the correct order depending on their first letter. Well, what if the next few letters are different? then you'll have to search those too! so, basically strcmp does this all for you, and very nicely too
so, for example:
strcmp("letter a","letter b");
would return -1, because string one is "less than" string two, and this works vice-versa too, and for any size of string! (it's a very handy function, you just have to know where to use it)
linuxdude wrote
You could doI always wondered this why does the strcmp int
<string.h>
return 0 if they compare equally so you cant do this
while(strcmp(str1,str2))
while(!strcmp(str1,str2))
which would mean the loop would keep going while the strings were equal.
hobo
>while(!strcmp(str1,str2))
You could...but the above can be confusing. Logical not is usually associated with false, so your loop is most easily read as "while str1 and str2 are not equal", which is the exact opposite of what you're really testing for. IMO it's better to be explicit when such confusion can occur:
Code:while ( strcmp ( str1, str2 ) == 0 ) /* While the strings are equal */Code:while ( strcmp ( str1, str2 ) != 0 ) /* While the strings are not equal */
My best code is written with the delete key.
I take your point Prelude.
It amazes me how many things that I've been taught are either not good practice or just plain wrong and I'm obviously not the only one, going by the numerous posts that I've read. That's what makes this site so invaluable.
Thanks Prelude
hobo