Hello

see strcmp(“hello”,”hello”) + nl +

strcmp(“abc”,”bcd”) + nl +

strcmp(“bcd”,”abc”) + nl

if the two strings are the same then it returns 0

abc and bcd aren’t the same. in the second line it returns -1 and in the third line it returns 1

in the second line we compare between “abc” and “bcd”

not equal because the first letter in “abc” = “a”

and the first letter in “bcd” = “b”

so we have “a” != “b” and “a” < “b”

so we get output = -1

In the third line we have “bcd” and “abc”

the first letter in “bcd” is “b” and the first letter in “abc” is “a”

so we have “b” != “a” and “b” > “a”

so we get output = 1

Note :

ASCII(“a”) = 97

and ASCII(“b”) = 98

So “a” < “b” because 97 < 98

Advertisements