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

Objective-C: Exclamation Point strcmp in "if" statement

I am looking over some OpenGL ES code to multiplay matrices, but I'm not sure about how this if statement works: for (int i = 0; i <_uniformArraySize; i++) { **if (!strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix")) {** …
foobar5512
  • 2,470
  • 5
  • 36
  • 52
2
votes
1 answer

error when I call strcmp Invalid conversion from 'int' to 'const char*'

I'm using strcmp to compare character arrays in c++, but I get the following error for every occurrence of strcmp: error: invalid conversion from 'int' to 'const char*' followed by: error: initializing argument 2 of 'int strcmp(const char*, const…
adhanlon
  • 6,407
  • 13
  • 43
  • 41
2
votes
1 answer

C Program - Palindrome with functions - almost fully complete one small issue

Is there a way I can remove the white spaces when comparing two strings. The assignment is create a palindrome that is case insensitive and must ignore the white spaces. So far I have void cmpNoCase(char str1[], char str2[]){ if(strcasecmp(str1,…
2
votes
2 answers

strcmp string and character array in c

Here is the code I have. I'm trying to do a string comparison. A serial input reads what keys are pressed and sets cmd.command to what was typed on the keyboard. Then I take that and do a string comparison to see if it is a command that's within my…
Jack
  • 5,264
  • 7
  • 34
  • 43
2
votes
1 answer

What's wrong with strcmp in this program?

When I run the program and answer "yes", it tells me that I am wrong. Does anybody know a way to execute this type of program? #include #include int strcmp(const char *str1,const char *str2 ); // I am trying to make a program…
2
votes
7 answers

When will strcmp not return -1, 0 or 1?

From the man page: The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. Example code in…
beatgammit
  • 19,817
  • 19
  • 86
  • 129
2
votes
2 answers

strcmp() and New Line Characters from Text File

I am attempting to use strcmp() to test whether a particular line in a text file is equal to a new line character (\n). I am gathering each line from one text file, and writing them to another text file. I do not want to write the line in the…
nairware
  • 3,090
  • 9
  • 37
  • 58
2
votes
2 answers

efficient and portable strcmp for reverse strings

strcmp, at least using g++, has many optimitzations for many architectures. In my pc, a Core2Duo E8400, strcmp is two times faster than use a straigforward implementation. My question if it exists some library that provides a function that compares…
user1476999
  • 177
  • 1
  • 8
2
votes
3 answers

Why is strcmp not returning 0 in this context?

So I'm reading in chars one by one from a file: char temp[3]; temp[0] = nextchar; printf("%c",temp[0]); //prints % temp[1] = nextchar = fgetc(srcptr); printf("%c",temp[1]); //prints 2 temp[2] = nextchar = fgetc(srcptr); printf("%c",temp[2]);…
varatis
  • 14,494
  • 23
  • 71
  • 114
1
vote
2 answers

Having trouble comparing strings in file to an array of strings inputted by user in C

I have tried to research this question, but was unable to find anything that would help me. I have been constantly trying to debug using fprint, but I still cannot figure it out. I am an intermediate programmer, and would love if I could get some…
1
vote
5 answers

strncpy char string issue when adding length

I'm having a problem with comparing 2 char strings that are both the same: char string[50]; strncpy(string, "StringToCompare", 49); if( !strcmp("StringToCompare", string) ) //do stuff else //the code runs into here even tho both strings are the…
codrgi
  • 211
  • 1
  • 4
  • 10
1
vote
2 answers

Asking a Yes or No query in MATLAB

I have written a script in MATLAB, where I am retrieving rows and columns from a table based on the WHERE clause. So far i manage to retrieve the data from the database table. The problem is that i would like to allow the user to have the option of…
Jeiman
  • 1,121
  • 9
  • 27
  • 50
1
vote
5 answers

Proper use of the strcmp function?

Can someone please explain to me how to properly use the strcmp function? I'm creating a tic-tac-toe game and I keep getting the error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast I've created two pointers that act as…
user1064913
  • 335
  • 2
  • 8
  • 19
1
vote
3 answers

What does "passing argument 2 of strcmp makes pointer from integer without a cast" error in C mean?

I am compiling my C code and am getting two errors: warning:passing argument 2 of strcmp makes pointer from integer without a cast and warning: note: expected const char * but argument is of type int This my main: int main(int argc, char…
user1013414
  • 9
  • 1
  • 1
  • 3
1
vote
6 answers

return value of function == 0?

Ok, so I have the following function: int functionX() { return strcmp(array1,array2)==0; } Why would anyone do this? the ==0 would suggest that this function will always return FALSE. Is this true or am I missing some exotic C syntax primers?
Evert
  • 563
  • 1
  • 5
  • 13