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
249
votes
11 answers

How do I properly compare strings in C?

I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this: #include int main() { char input[40]; char check[40]; …
nmagerko
  • 6,586
  • 12
  • 46
  • 71
143
votes
8 answers

Is there a JavaScript strcmp()?

Can anyone verify this for me? JavaScript does not have a version of strcmp(), so you have to write out something like: ( str1 < str2 ) ? -1 : ( str1 > str2 ? 1 : 0 );
jonathan
41
votes
8 answers

Why is strcmp not SIMD optimized?

I've tried to compile this program on an x64 computer: #include int main(int argc, char* argv[]) { return ::std::strcmp(argv[0], "really really really really really really really really really" "really really really really…
user1095108
  • 14,119
  • 9
  • 58
  • 116
34
votes
4 answers

Is the strcasecmp algorithm flawed?

I am trying to reimplement the strcasecmp function in C and I noticed what appears to be an inconsistency in the comparison process. From man strcmp The strcmp() function compares the two strings s1 and s2. The locale is not taken into account (for…
Haltarys
  • 577
  • 4
  • 10
33
votes
2 answers

Inconsistent strcmp() return value when passing strings as pointers or as literals

I was playing around with strcmp when I noticed this, here is the code: #include #include int main(){ //passing strings directly printf("%d\n", strcmp("ahmad", "fatema")); //passing strings as pointers char…
Ahmad AL-wazzan
  • 281
  • 3
  • 7
31
votes
8 answers

strcmp for empty string

I was reviewing some code and I saw someone do a if (0 == strcmp(foo,"")) I am curious because I think it would be faster to do a if (foo[0] == '\0') Is this correct or is strcmp optimized enough to make them the same. (I realize that even if…
Pablitorun
  • 1,035
  • 1
  • 8
  • 18
25
votes
7 answers

Why does strcmp() return 0 when its inputs are equal?

When I make a call to the C string compare function like this: strcmp("time","time") It returns 0, which implies that the strings are not equal. Can anyone tell me why C implementations seem to do this? I would think it would return a non-zero…
Xenu
  • 427
  • 1
  • 5
  • 10
24
votes
6 answers

lvalue required as left operand of assignment

Why am I getting lvalue required as left operand of assignment with a single string comparison? How can I fix this in C? if (strcmp("hello", "hello") = 0) Thanks!
Joseph
  • 401
  • 2
  • 6
  • 10
22
votes
5 answers

strcmp() return values in C

I am learning about strcmp() in C. I understand that when two strings are equal, strcmp returns 0. However, when the man pages state that strcmp returns less than 0 when the first string is less than the second string, is it referring to length,…
dmubu
  • 1,073
  • 4
  • 16
  • 18
22
votes
6 answers

Why are standard string functions faster than my custom string functions?

I decided to find the speeds of 2 functions : strcmp - The standard comparison function defined in string.h xstrcmp- A function that has same parameters and does the same, just that I created it. Here is my xstrcmp function : int xstrlen(char…
user1151738
21
votes
3 answers

Why is strcmp so much faster than my function?

I wrote a function, Str::Compare, that is basically a strcmp rewritten in another way. While comparing the two function, in a loop repeted 500'000'000 times, strcmp execute too much fast, about x750 times faster. This code was compiled in a C…
EnryFan
  • 422
  • 3
  • 15
20
votes
5 answers

What does strcmp return if two similar strings are of different lengths?

I understand that if you have 'cat' (string1) and 'dog' (string2) in strcmp (this is a C question) then the return value of strcmp would be less than 0 (since 'cat' is lexically less than 'dog'). However, I am not sure what would happen with strcmp…
Daniel Soutar
  • 827
  • 1
  • 10
  • 24
19
votes
1 answer

Why does strcmp() in a template function return a different value?

I am reading again "C++ Primer, 5th edition". In chapter 16 about templates, there is an example of "template Non-type parameters": template int compare(const char (&p1)[N], const char (&p2)[M]) { return strcmp(p1,…
Maestro
  • 2,512
  • 9
  • 24
17
votes
9 answers

Am I correct that strcmp is equivalent (and safe) for literals?

We all know the trouble overflows can cause, and this is why strn* exist - and most of the time they make sense. However, I have seen code which uses strncmp to compare commandline parameters like so: if(... strncmp(argv[i], "--help", 6) == 0 Now,…
Draemon
  • 33,955
  • 16
  • 77
  • 104
16
votes
3 answers

What's wrong with strcmp?

In the responses to the question Reading In A String and comparing it C, more than one person discouraged the use of strcmp(), saying things like I also strongly, strongly advise you to get used to using strncmp() now, ... to avoid many problems…
David Cary
  • 5,250
  • 6
  • 53
  • 66
1
2 3
63 64