[Tutorial] Strcmp explained/how does it work exactly.
#1

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:
pawn Код:
if(strcmp("Hello", "Hello") == 0)
{
    // Do something if they the same
}
else
{
    // Do something if they are different from another
}
If the 2 strings are not equal to eachother, strcmp will return the result from the substractions.

~I hope this helped in any way
Reply
#2

At first I thought this was another strcmp/strtok command tutorial, but I'm glad it isn't. I actually never knew how strcmp actually works, just that it works. I learnt something new today.
Reply
#3

Very Nice
I knew how it worked, i just didn't see WHY it returned 0 - but now i see
Reply
#4

I did see why it returned 0 on success, but I was wondering why it could either return -1 or 1. Could you explain that as well?
Reply
#5

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
I did see why it returned 0 on success, but I was wondering why it could either return -1 or 1. Could you explain that as well?
Isn't it -1 if the strings don't match but the first string comes before the second? and vice-versa, 1 for the strings being the other way round?
Reply
#6

Nice tutorial.

I have learnt something new today.
Reply
#7

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
I did see why it returned 0 on success, but I was wondering why it could either return -1 or 1. Could you explain that as well?
Yes, ASCII-Codes are like numbers, every character has a number. If you compare a character that has a lower ASCII value then the one you're comparing it to has (for example: 5 - 7 = -2), it will return a negative result. Same applies for whole strings. I think PAWN just rounds any values under 0 to -1 and any values above it to 1.

Quote:

At first I thought this was another strcmp/strtok command tutorial, but I'm glad it isn't. I actually never knew how strcmp actually works, just that it works. I learnt something new today.

Thank you, glad I could help
Reply
#8

Quote:
Originally Posted by Sinner
Посмотреть сообщение
Yes, ASCII-Codes are like numbers, every character has a number. If you compare a character that has a lower ASCII value then the one you're comparing it to has (for example: 5 - 7 = -2), it will return a negative result. Same applies for whole strings. I think PAWN just rounds any values under 0 to -1 and any values above it to 1.
Already though that would be it after reading it, but thanks for confirming it
Reply
#9

Sinner thanks very much. I own 2 well scripted servers, and I did not know how strcmp works. But today I do thanks.
Reply
#10

I dont really know much about strcmp and so far my servers came along very well without it but now I have hit a deadend where I need to learn it, I understand this part but I need a more in depth tutorial before understanding this perfectly
Reply
#11

Mind if I add on to this?
Reply
#12

Quote:
Originally Posted by Schurman
View Post
Mind if I add on to this?
Ofcourse not, be my guest.
Reply
#13

Simple, but taught me alot.
Reply
#14

Very good explanation! I now understand that when comparing passwords, i have to make sure the result is 0 to pass the check, and that either 1 or more, or -1 or less is a wrong password. Thanks a lot!
Reply
#15

GREAT! 10/10
Reply
#16

Just to make it a little more clear strcmp returns -1, if the first string precedes the second string alphabetically. The wiki doesn't realy explain it (return values) clear enough IMO.

pawn Code:
strcmp("aardvark", "badman");//will return -1
strcmp("aardvark", "aardvark");//will return 0
strcmp("badman", "aardvark");//will return 1
Reply
#17

Nice tutorial. Now everyone knows how to do it.
5/5!!
Reply
#18

Very Nice I also didn't knew Why it returned 0. Thanks!
Reply
#19

I learned something today about strcmp.

Thank you for this tutorial.
Reply
#20

Thanks, this helped alot!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)