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

Understanding the strcmp function of gnu libc

Here is the strcmp function that i found in the glibc: int STRCMP (const char *p1, const char *p2) { const unsigned char *s1 = (const unsigned char *) p1; const unsigned char *s2 = (const unsigned char *) p2; unsigned char c1, c2; do { …
localhost
  • 146
  • 10
5
votes
2 answers

Templated Functions.. ERROR: template-id does not match any template declaration

I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works…
Pavitar
  • 4,282
  • 10
  • 50
  • 82
5
votes
6 answers

Error in strcmp() when I use a structure as a parameter

My program needs these functionalities: NOTE: I did not include the codes for the numbers 1,2 & 4 since I already finished them. The 3rd one is my problem. The program should continuously allow input from the user as long the user still wants to.…
PG20
  • 67
  • 1
  • 2
  • 10
5
votes
12 answers

C++ - strcmp() does not work correctly?

There's something really weird going on: strcmp() returns -1 though both strings are exactly the same. Here is a snippet from the output of the debugger (gdb): (gdb) print s[i][0] == grammar->symbols_from_int[107][0] $36 = true (gdb) print s[i][1]…
Onur Cobanoglu
  • 211
  • 1
  • 6
  • 9
5
votes
4 answers

strcmp() thinks that strings arent equal.. but are they?

For unknown reason, result of running my C program is quite unexpected. I think that it has to be some kind of a beginner mistake, however I can't find out really, where is it. #include #include int main() { char…
hardpenguin
  • 159
  • 1
  • 2
  • 9
5
votes
2 answers

C Beginner: How to search for particular words in a file (line by line) in C

I need to search for two particular words in a file line by line and if they exist, print "Found!". This is file.txt (has four columns) bill gates 62bill microsoft beyonce knowles 300mill entertainment my name -$9000 student The following is the…
4
votes
2 answers

Why do these two programs give different outputs in VC++2008?

Why do these two programs give different outputs in VC++2008? After all, the same strings are compared. strcmp__usage.c #include #include main() { char targetString[] = "klmnop"; printf ("Compare = %d\n", strcmp(targetString,…
user366312
  • 16,949
  • 65
  • 235
  • 452
4
votes
8 answers

Why comparing 2 strings doesn't give desired result?

Below is the code I've come up with and in all honesty tonight is my first attempt at coding at all. However I cannot seem to get my if statement to work properly. It just simply jumps to else even if I type Westley or westley or (space)Westley. …
user968683
  • 65
  • 1
  • 4
4
votes
1 answer

Subtracting two characters

I just started programming in assembly so I am a beginner. To practice, I am trying to rewrite a basic libc in assembly (NASM Intel syntax). But I'm stuck on the strcmp function: ;; Compare two C-style NUL-terminated strings ;; Inputs : ESI =…
user14032812
4
votes
3 answers

Ambiguous behaviour of strcmp()

Please note that I have checked the relevant questions to this title, but from my point of view they are not related to this question. Initially I thought that program1 and program2 would give me the same result. //Program 1 char *a = "abcd"; char…
xrfxlp
  • 421
  • 5
  • 15
4
votes
1 answer

GDB - strcmp not working: __strcmp_sse2_unaligned

I'm unable to create conditional breakpoint in GDB using strcmp: break x if strcmp(str.c_str(), "foo") == 0 Why you ask? Because: print strcmp("hello", "hello") Yield's (int (*)(const char *, const char *)) 0x7ffff76ffe70 <__strcmp_sse2_unaligned>…
Master Wo
  • 215
  • 2
  • 7
4
votes
3 answers

Strcmp not comparing strings from argv

** Updated 26/10 -> First of all thank you all for you help, I am getting closer now, I need more work and studying but I really appreciate you are helping me a lot :-) Still don't know why the first "rain" word in the input.txt file is not getting…
Jess
  • 85
  • 12
4
votes
5 answers

Inlining this function or not?

I'm supposed to implement a function which compares two strings simliar so strcmp but ignoring whitespace characters, so strcmpignorews("abc ", " a b c") should give the same result. Here's my implementation: namespace { void…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
4
votes
5 answers

C - strcmp related to an if statement

In the code below I use strcmp to compare two strings and make this comparison the condition of an if statement. With the code below, the output will be hello world, because string "one" is equal to string "two". #include #include…
scugn1zz0
  • 301
  • 1
  • 6
  • 15
4
votes
1 answer

Insert function for BST with strings in C

This is BST NODE struct BST_node{ char *name1; char *data1; struct BST_node* left; struct BST_node* right; }; struct BST_node* Insert(struct BST_node *rootptr, datatype_t *d){ if(rootptr == NULL){ char name[66]; char…
Dave
  • 71
  • 1
  • 10