Questions tagged [strcmp]

strcmp is a string compare function that is available in languages such as C, C++, PHP, Python and MATLAB.

strcmp is a string compare function that is available in languages such as C, C++, PHP, Python and MATLAB.

Structure

PHP
int strcmp ( string $str1 , string $str2 )
C
int strcmp( const char *lhs, const char *rhs );

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Examples

PHP
echo strcmp("a","z"); // returns a negative number
echo strcmp("a","a"); // returns 0
echo strcmp("z","a"); // returns a positive number
C
int res = strcmp("a", "z"); // result is a negative number
int res = strcmp("a", "a"); // result is 0
int res = strcmp("z", "a"); // result is a positive number

References

https://en.cppreference.com/w/cpp/string/byte/strcmp

949 questions
10
votes
4 answers

Compare between a char in a string to a given char

I have the following: int findPar(char* str) { int counter=0; while (*str) { if(str[0] == "(") <---- Warning { counter++; } else if (str[0]== ")") <---- Warning { counter--; } if (counter<0) { …
Itzik984
  • 15,968
  • 28
  • 69
  • 107
9
votes
1 answer

Swift why strcmp of backspace returns -92?

I was tried to detecting backspace inside of UITextfieldDelegate. And found this answer. https://stackoverflow.com/a/49294870/911528 And It working correctly. But I don't know what's going on inside of this function. let char =…
AutumnSky
  • 394
  • 2
  • 14
9
votes
2 answers

c - strcmp not returning 0 for equal strings

So I've tried searching for a solution to this extensively but can only really find posts where the new line or null byte is missing from one of the strings. I'm fairly sure that's not the case here. I am using the following function to compare a…
Xephz
  • 103
  • 1
  • 4
9
votes
4 answers

How to properly compare command-line arguments?

I am trying to write a C code which takes arguments in main; thus when I write some strings in cmd, the program doing somethings inside it. But I am doing something wrong and I can't find it. This is the code: #include #include…
stuck
  • 1,477
  • 2
  • 14
  • 30
9
votes
5 answers

strcmpi renamed to _strcmpi?

In MSVC++, there's a function strcmpi for case-insensitive C-string comparisons. When you try and use it, it goes, This POSIX function is deprecated beginning in Visual C++ 2005. Use the ISO C++ conformant _stricmp instead. What I don't see is…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
8
votes
3 answers

Loadable Bash Builtin

I'm writing a strcmp bash builtin. It compiles fine, but when I try to enable it, I get: $ enable -f ./strcmp strcmp bash: enable: cannot open shared object ./strcmp: ./strcmp: only ET_DYN and ET_EXEC can be loaded The big parts of my…
David Souther
  • 8,125
  • 2
  • 36
  • 53
8
votes
1 answer

strcmp cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*

Apologies in advance for the elementary nature of the question. I am trying to use the strcmp function to test two strings for matching characters. I reduced the issue to the simple code below: #include #include using…
user3672337
8
votes
4 answers

C - Comparing string literal with character array

I am new to C and am still a bit confused about how to use strings via character arrays. In my C program, I am accepting commands from the user: char command[20]; scanf("%s",command); Of course, afterwards I want to figure out what command they…
Irina
  • 1,333
  • 3
  • 17
  • 37
8
votes
6 answers

strcmp with pointers not working in C

Why this code isn't working. Just trying to check if the user input is the same as a password char *pass; printf("Write the password: "); scanf("%s", pass); // Because is a pointer the & is out ? if( strcmp( pass , "acopio") == 0)
jotape
  • 319
  • 2
  • 6
  • 14
7
votes
6 answers

C - Comparing numeric strings

Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C? #include #include #include int main(void){ char str1[5] = "123"; char str2[5] = "123"; char…
Valdogg21
  • 1,151
  • 4
  • 14
  • 24
7
votes
6 answers

strcmp() but with 0-9 AFTER A-Z? (C/C++)

For reasons I completely disagree with but "The Powers (of Anti-Usability) That Be" continue to decree despite my objections, I have a sorting routine which does basic strcmp() compares to sort by its name. It works great; it's hard to get that one…
Aaron Burke
  • 486
  • 3
  • 12
7
votes
5 answers

How to compare two strings case and diacritic-insensitive?

I have two strings String 1: "sebastien" String 2: "Sébastien" I want to compare these two strings by ignoring é (Accents) character. Can anyone know this logic? Thanks in advance
Srividhya
  • 389
  • 1
  • 4
  • 15
7
votes
6 answers

Does strlen() in a strncmp() expression defeat the purpose of using strncmp() over strcmp()?

By my understanding, strcmp() (no 'n'), upon seeing a null character in either argument, immediately stops processing and returns a result. Therefore, if one of the arguments is known with 100% certainty to be null-terminated (e.g. it is a string…
cvp
  • 73
  • 1
  • 3
6
votes
4 answers

Strcmp for cell arrays of unequal length in MATLAB

Is there an easy way to find a smaller cell array of strings within a larger one? I've got two lists, one with unique elements, and one with repeating elements. I want to find whole occurrences of the specific pattern of the smaller array within the…
Doresoom
  • 7,398
  • 14
  • 47
  • 61
6
votes
5 answers

Finding unique elements in an string array in C

C bothers me with its handling of strings. I have a pseudocode like this in my mind: char *data[20]; char *tmp; int i,j; for(i=0;i<20;i++) { tmp = data[i]; for(j=1;j<20;j++) { if(strcmp(tmp,data[j])) //then except the…
LuckySlevin
  • 325
  • 2
  • 7
  • 13
1 2
3
63 64