23.04.2011, 11:11
(
Последний раз редактировалось Sinner; 23.04.2011 в 17:10.
)
I've seen many people asking "how can I create a command with strcmp? (other command processors aside). Fact is, if you know exactly how strcmp works you wouldn't need to ask such a question in the first place, because it would all be so logical. This is true for all functions, if you know how they work it can really help you while you're programming. So here's a little 'noob' tutorial on how strcmp works
1. What is strcmp?
- StrCmp stands for String Compare, and all it does is compare 2 strings with eachother.
2. How does it NOT work?
- At first it seems logical to think that, if 2 given strings are equal to eachother, strcmp will return "1" as a result, and if the 2 strings are not equal to eachother, it will return "0". This is wrong! Exactly here is where most badly informed scripters go wrong.
3. How does it work
- strcmp works by taking each characters of a string, and substract it's ASCII-Code with the ASCII-Code of the character in the other string. So lets say you want to compare the string "hello" with the string "hello" (which is the same). This is what strcmp will do:
- Take the first character ('h') and substract it with the character from the second string ('h')
- If the characters are the same, the result of the substraction will be 0 (null)
- Do this for all characters of the strings
Now, if the 2 strings are the same, the total of all the characters substracted from eachother will be 0.
This is why to compare 2 strings you need to check if the result is 0, not 1.
Example:
If the 2 strings are not equal to eachother, strcmp will return the result from the substractions.
~I hope this helped in any way
1. What is strcmp?
- StrCmp stands for String Compare, and all it does is compare 2 strings with eachother.
2. How does it NOT work?
- At first it seems logical to think that, if 2 given strings are equal to eachother, strcmp will return "1" as a result, and if the 2 strings are not equal to eachother, it will return "0". This is wrong! Exactly here is where most badly informed scripters go wrong.
3. How does it work
- strcmp works by taking each characters of a string, and substract it's ASCII-Code with the ASCII-Code of the character in the other string. So lets say you want to compare the string "hello" with the string "hello" (which is the same). This is what strcmp will do:
- Take the first character ('h') and substract it with the character from the second string ('h')
- If the characters are the same, the result of the substraction will be 0 (null)
- Do this for all characters of the strings
Now, if the 2 strings are the same, the total of all the characters substracted from eachother will be 0.
This is why to compare 2 strings you need to check if the result is 0, not 1.
Example:
pawn Код:
if(strcmp("Hello", "Hello") == 0)
{
// Do something if they the same
}
else
{
// Do something if they are different from another
}
~I hope this helped in any way