Answer the question
In order to leave comments, you need to log in
How can strings be compared in C?
Greetings! Can you please tell me why this code is not working? What exactly did I do wrong?
Compilation
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
int a;
char str_1[30];
char str_2[30];
scanf("%s\n", str_1);
scanf("%s", str_2);
a = stricmp(str_1, str_2);
printf("%d", a);
return 0;
}
Answer the question
In order to leave comments, you need to log in
For safer handling of strings in C, it is better to use the strn family of functions, such as str n cmp().
Because stricmp is some unknown beast.
There is a standard strcmp - it does not ignore case and searches for the end of the string by '\0', which very often leads to SIGSEGV :)
There is strncmp - it also does not ignore case, but compares no more than N characters, which is considered safer if you forgot '\0' put.
There are strcasecmp and strncasecmp - complete analogues, only case is ignored.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question