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

Why does my version of strcmp not work?

I have a my own version of strcmp that looks like this int strcmp(char str1[], char str2[]) { int i = 0; while ((str1[i] == str2[i]) && (str1[i] != '\0')) { i++; } if (str1[i] > str2[i]) return 1; if…
user3328187
  • 193
  • 2
  • 2
  • 6
4
votes
3 answers

get and check Strings in c language

I tried to write program that scan a string from user and check it what the user input and if it is true do somthing and if it's not do somthing else. the code i wrote is like this: #include #include int main() { char…
amin
  • 51
  • 4
4
votes
3 answers

Can strcmp() work with strings in c++?

I have this line of code if(strcmp(ob[i].getBrand(), ob[j].getBrand()) > 0) and I get this error error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' Does that mean that strcmp doesnt work with strings, but…
4
votes
4 answers

Is this the only return value for strcmp() in C?

I'm learning C, and am currently studying String Handling. From where I'm studying, strcmp() is defined as- This is a function which compares two strings to find out whether they are same or different. The two strings are compared character by …
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
4
votes
4 answers

Comparing two strings, problems with strcmp

I'm trying to check if the line read from stdin begins with "login:" but strcmp does not seem to work. char s1[20], s2[20]; fgets(s1, 20, stdin); strncpy(s2,s1,6); strcmp(s2, "login:"); if( strcmp(s2, "login:") == 0) printf("s2 =…
Barsan Ionut
  • 45
  • 1
  • 1
  • 6
4
votes
2 answers

Program errors due to strcmp and strcpy in C

No matter how I edit my program there seems to be overflow errors and mismatching type errors. Can someone help me to make this run without errors. #include #include #include int main() { int choice; int i; …
3
votes
0 answers

strcmp in gdb giving odd results

In GDB (gnu v 7.1-ubuntu) I am getting really weird results when I try to use strcmp to determine if two strings are equal. p strcmp("hello","hello") is giving me the result -145947168. Everything I try with strcmp or strncmp is returning -145947168…
John St. John
  • 1,542
  • 1
  • 13
  • 22
3
votes
1 answer

alphanumeric sorting order of strings in C

Possible Duplicate: Natural sort in C - “array of strings, containing numbers and letters” When sorting strings in C with qsort and strcmp I have the problem that alphanumeric entries, typically strings ending with numbers, are being sorted oddly…
Chris
  • 981
  • 4
  • 14
  • 29
3
votes
1 answer

Strcmp in nasm x86_64, registers

I am trying to implement my own strcmp in asm. Here is the ft_strcmp.s file: global ft_strcmp section .text ft_strcmp: mov eax, [rdi] sub eax, [rsi] jne .exit cmp byte [rdi], 0 ; if s1 end …
mondrew
  • 33
  • 4
3
votes
3 answers

comparing the ending of the strings

I am writing a program to compare different strings. Specifically chemical elements that end with an OH. I have to return -1 if the string ends with OH. However, my program doesn't work. Where am I wrong? #include #include int…
user13442873
3
votes
4 answers

The Exclamation Notation and `strcmp` Function in C programming

There is a question that makes me confused during learning with the Harvard University CS50 course. The following is the question that bothers me for a long time. For the following code, it wants to compare the string called "EMMA" with the array…
Ricky.L
  • 47
  • 4
3
votes
7 answers

comparing using strcmp

compiling with gcc C99 I am trying to compare 2 string using string compare. However, I seem to be getting a stack dump on the strcmp line. **attribute will contain these, so I am looking for frametype. [name] [time] [type] [time] [name] [callref]…
ant2009
  • 27,094
  • 154
  • 411
  • 609
3
votes
1 answer

How to make strcmp to return 0 in assembly

I want the call to the strcmp function to return 0, which means int strncmp(const char *s1, const char *s2, size_t n); const char *s1 and const char *s2 should contain the same string. If s2 points to the string "hello" and n is 4, how can I pass…
Rio
  • 14,182
  • 21
  • 67
  • 107
3
votes
2 answers

PHP strcmp result int meaning

In php, there is a built-in function strcmp to compare if two strings are same or not. Returning value is integer number that if the first parameter is greater than the second I get > 0, if not < 0 and if the same 0. So the part I don't get is…
norixxx
  • 2,549
  • 2
  • 19
  • 26
3
votes
2 answers

Using strcmp() in structures passed as pointers

I have a bunch of practice questions in Structures, all of which involve structures passed as pointers to function arguments. Now, I have a specific question at hand, which asks me to store some names and telephone numbers in a phone book structure.…