V
V
Vladimir Rudometkin2018-11-21 19:36:12
C++ / C#
Vladimir Rudometkin, 2018-11-21 19:36:12

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

2 answer(s)
F
FD4A, 2018-11-21
@FD4A

For safer handling of strings in C, it is better to use the strn family of functions, such as str n cmp().

C
CityCat4, 2018-11-22
@CityCat4

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 question

Ask a Question

731 491 924 answers to any question