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
3
votes
3 answers

Mystery with strcmp output - How strcmp actually compares the strings?

I want to know why strcmp() returns different values if used more than once in the same function. Below is the program. The first case I am aware of why it prints -6. But in the second case, why does it print -1?…
3
votes
2 answers

Non Null-terminated value causing StrCmp to return 0?

I have the following code: _Bool grantAccess(char *password){ char goodPassWord[]= "goodpass"; return (0 == strcmp(password, goodPassWord)); } _Bool grantAccessExercise(void){ char password[9]; int allow = 0; printf("Please…
qz_99
  • 185
  • 1
  • 12
3
votes
2 answers

Comparing user input to a predefined array of characters in C programming language

I have the following code which is supposed to get user input as commands then check whether the command is predefined. However, for any command entered the output is "You asked for help". I figured that the problem might be related to the way I'm…
pete mwangi
  • 61
  • 1
  • 7
3
votes
2 answers

returning 0 if strings are same in c, where is happening in the code?

I found a function which performs the same as strcmp but I was unable to see where the comparison s1 == s2 is happening. I need help. Thanks. int MyStrcmp (const char *s1, const char *s2) { int i; for (i = 0; s1[i] != 0 && s2[i] != 0; i++) …
J Pi
  • 41
  • 3
3
votes
1 answer

memcmp / strcmp vs uint64_t comparisson

I have lots of strings each with size 8 or less. I need to do lots of comparisons there using memcmp() / strcmp(). I wonder if comparisons will work faster if I convert all them to std::uint64_t. In this case, at least on theory comparison will be…
Nick
  • 9,962
  • 4
  • 42
  • 80
3
votes
2 answers

Is #define var bad with strcmp?

Can i compare#definevarible andchar * in strcmp as below. #include #include #define var "hello" int main() { char *p ="hello"; if(strcmp(p,var)==0) printf("same\n"); else printf("not same\n"); return 0; } Is there any risk…
Rohini Singh
  • 297
  • 1
  • 14
3
votes
1 answer

scanf isn't executing on every loop iteration

I am writing a program for fun (not for school), and am having a hard time figuring out why the scanf function isn't executing on every iteration of my loop - I've toyed with both 'for' loops and 'while' loops. I know that depending on how I write…
blarpsplat
  • 33
  • 4
3
votes
2 answers

C programming: Unexpected results from strcmp after using getline

I am writing a C program which will take a list of commands from stdin and exec them. I am having unexpected results from using strcmp after reading in from stdin. Here is my program test_execvp.c #include #include #include…
3
votes
3 answers

Why does using == on a C-style string work?

I was under the impression that comparison operators are not defined for C-style strings, which is why we use things like strcmp(). Therefore the following code would be illegal in C and C++: if("foo" == "foo"){ printf("The C-style comparison…
3
votes
3 answers

Login Script does not compare string inputs as expected

So i want to make a login script and I have coded it but when I run it, it always says "Wrong Username" even though it is right. This is my code #include #include #define MYNAME "SLITHER" //Defines SLITHER so u can put %s yo…
slither
  • 25
  • 2
3
votes
1 answer

Getting segmentation fault using qsort and simple strcmp function in C?

GDB print commands suggest that my array and its contents are formatted correctly ('asdf' , '\00000') but I get a segfault in in my qsort call that traces back to the comparison function I pass it. All I can surmise is that I am somehow passing…
Harrison
  • 319
  • 2
  • 14
3
votes
1 answer

Read a character from file with C and compare to check if it is an asterisk

I am trying to finally learn C but I am getting stuck with some stupid things. In this case, what I am trying to achieve, is to create a program that will manage phone contacts. The "tool" allows to insert a contact or delete and search for…
Rodrigo
  • 91
  • 8
3
votes
2 answers

How to do a case-insensitive string comparison?

I want to do a case-insensitive string comparison. What would be the easiest way to accomplish that? I have the below code which performs a case-sensitive operation. #include #include #include int main(int argc, char…
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
3
votes
0 answers

How to slow down program execution

I have a simple executable binary. It takes as input a user supplied string and compares it with a private string using strcmp. How can I slow down the execution of this program such that I can launch a statistical timing attack on the string…
3
votes
1 answer

Why doesn't strcmp recognize two seemingly equal strings as the same?

Below is the output from the Matlab's console. Both of the strings are the same: '@TBMA3'. Yet Matlab's strcmp function returns 0 when comparing them. Why? K>> str='@TBMA3' str = @TBMA3 K>> method.fhandle ans = @TBMA3 K>>…
cerebrou
  • 5,353
  • 15
  • 48
  • 80