I have the following code:
printf("num: %d\n", strcasecmp(buf, "h\n"));
And I get the following results when I try plugging in different letters:
a: -7
g: -1
i: 1
j: 2
h: 156
H: 156
Should strcasecmp not return 0 when buf is equal to H or h? Any ideas why it’s returning 156? I need to figure out how to check whether the user types H or h.
Thanks!
Edit: I’m reading buf in the following way:
read(0, buf, MAXBUFLEN);
Why
\nat the end, if you want to compare withhorH?read()also stores whitespace. So, if you are usingread, compare with “h\n”.I entered h in the above case.
Also, use
strncasecmpif you want to compare a fixed number of characters.