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
1 answer

Why is strcasecmp not working?

I am doing a string comparison in php, but strcasecmp always outputs that the strings are not equal. Here is my code:
user3709132
  • 21
  • 1
  • 7
-3
votes
1 answer

My string compare is coming out wrong

hi my program is to enter a number which gives the length of the string then the string and then finally a letter which should then tell me how many times that letter is in the string. Currently to help me figure out what is wrong with my code i can…
ltbrown
  • 1
  • 1
-3
votes
2 answers

If and else if with strcmp sequence fault / overlapping

at first I'll post the needed code. #define STRING_LEFT "05000858FB" #define STRING_RIGHT "05000B3E45" #define STRING_LENGTH_MAX 16 char stringname[STRING_LENGTH_MAX+1]; if(strcmp(stringname, STRING_LEFT)==0) { // do stuff A…
-3
votes
2 answers

compare strings with strcmp function works different

I'm comparing two strings with strcmp in the following manner: long t=1011; char tc[10], tcr[10]; ltoa(t,tc,10); cout<
Khuram
  • 53
  • 4
-3
votes
1 answer

My strcmp doesn't return 0 when it should

Context : I'm searching for all the words contained in a 2d array (horizontally,vertically and diagonaly). So what I do is I get all the possible words, check if they're in the given dictionary and if they are store them in an array. The thing is, I…
user3690823
  • 35
  • 1
  • 2
  • 5
-3
votes
1 answer

Is this the right statement for string comparison of two strings?

if(strcmp(p[pas].origin,"West")&&strcmp(p[pas].destination,"East")==0) { cost[pas]=5.62+5.62; } is this the right code for the two input strings? or is there something wrong since the program runs but i cannot get the return…
mr.trey
  • 9
  • 2
-3
votes
4 answers

strcmp not working?

I have a strcmp function as so: if (strcmp(userInput, "Yes") == 0) For some reason, it will not enter the if statement, even though I am sure the userinput definitely equals Yes. Anyone have any clue whats wrong?
AkshaiShah
  • 5,739
  • 11
  • 37
  • 45
-4
votes
1 answer

Am I 'C'eriously going insane?

Ok so here we are comparing the memory location of the pointer to the string literal name[10] or Michael (or something like that) char name[10]="michael"; char name2[10]="michael"; if (name == name2) { printf("ok\n"); printf("OKAF\n"); } And…
-4
votes
3 answers

Comparing Multiple Strings Using Logical Operator Is Not Working Properly in C Programming

I am trying to check if the user string input (after lowercasing user input) matches with required three strings i.e. rock or paper or scissor. If it doesn't match the requirement the system will print It is a wrong input. Otherwise, I'll do…
-4
votes
2 answers

compare two binary files using strcmp() in c Language

Sorry for my bad English first. I have two binary files. And I store binary into buffer respectively. Then I compared two buffer using strcmp(). Result of strcmp() is zero. So I think two binary is identical. Open two binary and then checked if…
Jinwoo Bae
  • 35
  • 7
-4
votes
2 answers

what is the cause of runtime error in the following code

I was trying to solve a problem( https://www.codechef.com/problems/COOK82A) on an online judge. Following is the code for the problem.When i submit the judge gives runtime error.Can anybody help me #include int main(void) { int…
-4
votes
1 answer

Does strcpy or strcat make unnecessary changes of the string when used within printf statement?

I ran this code: #include #include int main() { static char str1[] = "dills"; static char str2[20]; static char str3[] = "Daffo"; int i,j; i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills"); printf("%d",…
Arnab
  • 19
  • 2
-4
votes
3 answers

Difference between "==" and "strcmp" in PHP

I was asked to create simulation array_keys function, but check equality "==" returns false. But with "strcmp ($a, $b) == 0" return true. class Utility { public function convertArrayKeys(array $array) { $i = 0; $elements =…
David
  • 15
  • 4
-4
votes
3 answers

strcmp call keeps crashing my program

I'm creating a "phonebook" program for school. Basically I have to get a user to input the names and phone numbers of their contacts into two different arrays. I got that part down, but after I have to give the user an option to search the contacts…
zman419
  • 39
  • 1
  • 1
  • 5
-4
votes
4 answers

Comparing an input string with a string that has a integer variable in C?

I'm trying to compare an input of characters with a string that can be of the format "!x" where x is any integer. What's the easiest way to do this? I tried int result = strcmp(input,"!%d"); which did not work.
sc1892353
  • 85
  • 1
  • 10