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
6 answers

Can I please get some feedback for this strcmp() function I implemented in C?

I'm learning C. I find I learn programming well when I try things and received feedback from established programmers in the language. I decided to write my own strcmp() function, just because I thought I could :) int strcompare(char *a, char *b) { …
alex
  • 479,566
  • 201
  • 878
  • 984
2
votes
1 answer

strcmp() not working as expected

echo($menuVal['icon']); /* if($menuVal['icon'] !== 'fa-dashboard' ){ $menuHTML .= ''; }*/ $cont=strcmp($menuVal["icon"],"fa-dashboard"); echo($cont); if((strcmp($menuVal["icon"],"fa-dashboard")) !=…
user6768630
2
votes
4 answers

C++ program crashes

I've this assignment to implement strcmp function. Sometimes it runs okay but other times it crashes. Please help me. #include using namespace std; int mystrcmp(const char *s1, const char *s2); int…
John
  • 23
  • 2
2
votes
7 answers

Baffled by strcmp

I have a very simple function to convert a 3 char string representing a bit string to a decimal number: int bin3_to_dec(char *bin) { int result; result=0; printf("string: %s\n", bin); printf("c0: %c\n", bin[0]); printf("c1: %c\n",…
ErwinM
  • 5,051
  • 2
  • 23
  • 35
2
votes
5 answers

Can I use strcmp as a boolean?

I am trying to use fgets to call different functions depending on what the string the user inputs. I know I will need to use strtok later since there will be spaces for, for example, "load 12". But now, I am confused about using strcmp to compare…
2
votes
1 answer

C strcmp not working

So I have the following code and basically buffer is supposed to hold a string, "NOT FOUND" which is given by the server I'm connecting to. It is done by the recvfrom() system call in the first function block. The problem is it is still going to my…
2
votes
4 answers

Why strcmp giving different response for complete filled character array?

#include #include void main() { char a[10]="123456789"; char b[10]="123456789"; int d; d=strcmp(a,b); printf("\nstrcmp(a,b) %d", (strcmp(a,b)==0) ? 0:1); printf("compare Value %d",d); } Output: strcmp(a,b) 0 …
Photon001
  • 145
  • 9
2
votes
2 answers

Int array empty - C

I am attempting to write a program that will take two sets of strings N and Q. The goal of the program is to print out the number of times each string in Q occurs in N. However, I am struggling to manage strings and pointers in C and in particular I…
JG7
  • 371
  • 2
  • 10
2
votes
1 answer

C: strcmp error while finding longest repeated substring

I'm trying to create a program which returns the Longest repeated substring. I've almost got the solution, but for some reason my strcmp gives an error when he is busy to find the LRS. Could someone explain me why there is an error and how I solve…
SpartanHero
  • 107
  • 8
2
votes
1 answer

How to make strcmp() work in my C program

I am about to make an encryption of a textstring, using another textstring as a "key" for my encryption. It is basically just a reorganization of the ASCII-characters. The key is given and is structured in a bad way, requiring some extra…
Carl
  • 101
  • 1
  • 11
2
votes
1 answer

string comparison in 8086

I have problem with this question. I don't know what it wants from me. Question : Write a procedure that compares a source string at DS:SI to a destination string at ES:DI and sets the flags accordingly. If the source is less than the destination,…
earlymorningtea
  • 508
  • 1
  • 9
  • 20
2
votes
2 answers

strcmp() returns wrong value even when the comparison is between same strings

#include #include #include using namespace std; int main() { string x; cin>>x; if(strcmp(&x.at(0), "M") == 0) { cout<<"midget "; } else if(strcmp(&x.at(0), "J") == 0) { …
Ivankovich
  • 119
  • 2
  • 8
2
votes
1 answer

Setting weightages for Jarowinkler in compare.linkage

I'm using compare.linkage method in the record linkage package in R to compare similarity of 2 set of strings. The default string comparing method is jarowinkler with the 3 default weightages set at 1/3, 1/3 and 1/3. I want to overwrite the default…
2
votes
1 answer

PHP's strcmp function giving weird outputs?

I have a file called users.txt, where I have lots of lines of username and password combinations. Here's an example of this file: kyle:pass1 steve:pass2 john:pass3 ralph:pass4 So a username would be steve and the password would be pass1. These are…
Kyle
  • 169
  • 7
2
votes
3 answers

How does strcmp work when comparing strings?

I am implementing a lexicographic sort and my professor told us to use strcmp in our implementation. The problem is, strcmp is very confusing in respects to how it compares strings. For instance, this here yields false: if (strcmp("What", "am")…
mrQWERTY
  • 4,039
  • 13
  • 43
  • 91