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
15
votes
4 answers

Efficient string sorting algorithm

Sorting strings by comparisons (e.g. standard QuickSort + strcmp-like function) may be a bit slow, especially for long strings sharing a common prefix (the comparison function takes O(s) time, where s is the length of string), thus a standard…
Piotr Turek
  • 346
  • 1
  • 2
  • 11
15
votes
8 answers

Is there any safe strcmp?

I made a function like this: bool IsSameString(char* p1, char* p2) { return 0 == strcmp(p1, p2); } The problem is that sometimes, by mistake, arguments are passed which are not strings (meaning that p1 or p2 is not terminated with a null…
P-P
  • 1,670
  • 5
  • 18
  • 35
15
votes
4 answers

Why is strcmp unknown to clang?

I have a basic program that compares two strings : #include #include using namespace std; int main (int argc, char *argv[]) { if(strcmp (argv[0],"./test") != 0) { cout << "not equal" << endl; } else { cout <<…
Barth
  • 15,135
  • 20
  • 70
  • 105
14
votes
3 answers

strcmp behaviour

When I run the following code: #include int main(int argc, char *argv[]) { int p = 0; p = strcmp(NULL,"foo"); return 0; } I get segmentation fault. echo $? says 139. But when I run #include int main(int argc,…
Ashish Vyas
  • 617
  • 1
  • 5
  • 19
13
votes
6 answers

What does strcmp() exactly return in C?

I wrote this code in C: #include #include #include #include int main() { char string1[20]; char string2[20]; strcpy(string1, "Heloooo"); strcpy(string2, "Helloo"); printf("%d",…
Akhil Raj
  • 457
  • 1
  • 4
  • 14
12
votes
5 answers

strcmp not working

I know this may be a totally newbie question (I haven't touched C in a long while), but can someone tell me why this isn't working? printf("Enter command: "); bzero(buffer,256); fgets(buffer,255,stdin); if (strcmp(buffer, "exit") == 0) return…
juan
  • 80,295
  • 52
  • 162
  • 195
12
votes
3 answers

Why does "echo strcmp('60', '100');" in php output 5?

PHP's documentation on this function is a bit sparse and I have read that this function compares ASCII values so... echo strcmp('hello', 'hello'); //outputs 0 as expected - strings are equal. echo '
'; echo strcmp('Hello', 'hello'); //outputs…
Rupert
  • 1,629
  • 11
  • 23
12
votes
2 answers

Compare part of an input string using strcmp() in C

Normally strcmp is used with two arguments [e.g. strcmp(str1,"garden")], and it will return 0 if both are the same. Is it possible to compare part of the input, say the first five character of the input? (for example,…
user1872384
  • 6,886
  • 11
  • 61
  • 103
12
votes
4 answers

Option Strict On disallows late binding

Can someone help me fix this error? Option Strict On disallows late binding Here's the code that's causing the error: Dim SF6StdData As BindingSource = New BindingSource() ' ... If StrComp(SF6StdData.Current("O2AreaCts").ToString, "") = 0 Then …
mike
  • 201
  • 1
  • 5
  • 13
11
votes
2 answers

strcmp() return different values for same string comparisons

char s1[] = "0"; char s2[] = "9"; printf("%d\n", strcmp(s1, s2)); // Prints -9 printf("%d\n", strcmp("0", "9")); // Prints -1 Why do strcmp returns different values when it receives the same parameters ? Those values are still legal since…
Bilow
  • 2,194
  • 1
  • 19
  • 34
11
votes
2 answers

Why use strcmp instead of == in C++?

I wonder my code works perfectly fine either using strcmp or simply == in C++ for comparing 2 char arrays. Can any one justify the reason of using strcmp instead of ==;
Fawad Nasim
  • 343
  • 2
  • 4
  • 10
11
votes
5 answers

Optimized strcmp implementation

This function was found here. It's an implementation of strcmp: int strcmp(const char* s1, const char* s2) { while (*s1 && (*s1 == *s2)) s1++, s2++; return *(const unsigned char*)s1 - *(const unsigned char*)s2; } I understand all…
Cody Smith
  • 2,732
  • 3
  • 31
  • 43
10
votes
2 answers

Segfault with strcmp

I am using strcmp in following ways Passing char[] array names Passing pointers to string literals but, the second result in seg fault. even though i have confirmed that pointers point to correct string literals, i am confused as to why i am…
Jimm
  • 8,165
  • 16
  • 69
  • 118
10
votes
8 answers

Implementation of strcmp

I tried to implement strcmp: int strCmp(char string1[], char string2[]) { int i = 0, flag = 0; while (flag == 0) { if (string1[i] > string2[i]) { flag = 1; } else if (string1[i] < string2[i]) { …
blackFish
  • 85
  • 1
  • 1
  • 8
10
votes
6 answers

strcmp on a line read with fgets

I'm trying to compare two strings. One stored in a file, the other retrieved from the user (stdin). Here is a sample program: int main() { char targetName[50]; fgets(targetName,50,stdin); char aName[] = "bob"; …
Blackbinary
  • 3,936
  • 18
  • 49
  • 62
1
2
3
63 64