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

strcmp not working in c

#include #include #include #include struct Position { char* x1[2]; char* x2[2]; }; int main(void) { struct Position positions[4]; struct Position p1[4]; char* p; …
Mar
  • 13
  • 6
-2
votes
1 answer

Efficiently comparing a relatively large number of strings with varying lengths

For a school project I wrote an x86 disassembler and just so I have something a bit more usable I'd like to make a complementary assembler. The problem is that I'm not really sure how I can efficiently compare the opcode to a list of char*s. Strcmp…
LUPE
  • 1
  • 3
-2
votes
2 answers

String compare in C

I'm a little confused with the string compare strcmp() function in C. When you have two strings, grass and grapes and you use strcmp(grass, grapes); which results in 39, or any positive number, does this mean that "grapes" is alphabetized before…
WTL
  • 103
  • 1
  • 3
  • 9
-2
votes
4 answers

In arrays, see if last four elements of 1st array match last 4 elements of 2nd array?

How is it possible to see if the last four elements of an array match the last four elements of the second array? For instance, I'm writing a password program. First, the user is asked their birth date. Then they are asked to input a password. for…
Quadruckus
  • 21
  • 4
-2
votes
1 answer

Weird Behaviour of strcmp [C]

Am I doing something wrong ? I'm trying to compare test and msg with strcmp. They are exactly the same but strcmp returns a negative value. I'm trying to code a stenography chat. #include #include #include void…
John Graig
  • 83
  • 1
  • 6
-2
votes
1 answer

(php) Is it correct how I understand strcmp()?

print("ABCDEF , ABC : " . strcmp("ABCDEF" , "ABC")); print("ABC , ABCDEF : " . strcmp("ABC" , "ABCDEF")); output: ABCDEF , ABC : 3 ABC , ABCDEF : -3 strcmp("ABCDEF" , "ABC") : A-A = 65-65 = 0, B-B = 66-66 = 0, C-C = 67-67 = 0, D-A = 68-65 =…
Kugan Kumar
  • 423
  • 1
  • 8
  • 14
-2
votes
2 answers

Segmentation fault strcmp in c

I'm trying to run a program in c that takes in a text file and string from the user and then searches the file for that string. It keeps getting a segmentation fault and gdb is pointing me towards this function but I am not sure what the problem…
-2
votes
1 answer

search a file with strcmp (run time error)

I have problem with my code , I get run time error. This code is suppose to ask the user to enter a name and then go through the file and search for the input entered by the user. and I got this warning. ( [Warning] passing argument 1 of 'strcmp'…
-2
votes
1 answer

Seg Fault when passing strings through void pointers in C

I am relatively new to programming, and this is my first term working in C. So it is entirely possible that this could be a really simple mistake, or it is also possible that the explanation of what I did wrong could go over my head. My program…
-2
votes
1 answer

if statement with nested functions will not execute

#include main() { char name[30]; int age; printf("Please enter your name: "); scanf("%s", name); printf("How old are you %s: ", name); scanf("%d", age); if (strcmp(name, "Abs") == 1 && age == 25) …
Abhijit Sikder
  • 1
  • 1
  • 1
  • 2
-2
votes
2 answers

strcmp() returns 0 when comparing to unequal strings

In C, strcmp() function returns 0 if two strings are equal. When I give a code like this, char str[10] = "hello"; if(strcmp(str,strrev(str))==0) { printf("1"); } else printf("0"); This should print a 1 if its a palindromic string or it should print…
-2
votes
2 answers

username and password validation turbo c

#include #include #include int main() { clrscr(); char username[15], password[15], ch, usr[15], pass[15]; int choice, i=0, found=0; printf("1.Add Admin"); printf("\n2.Log-in Admin"); printf("\n3.Exit"); …
Dme12
  • 3
  • 1
  • 4
-2
votes
1 answer

Should these two cstrings of different sizes be returning 0(equal) with strcmp?

I have two classes that will create a dynamically allocated cstring (null terminated) of n size upon an object of that class being created. In one class, I have a member function overloading the equivalency operator, and in the other class, I have a…
Victor
  • 57
  • 2
  • 8
-2
votes
1 answer

Comparing strings with strcmp()

The code is: #include int main() { const char *str1{"Jill"}; const char *str2{"Jacko"}; int result{std::strcmp(str1, str2)}; if(result < 0) { std::cout << str1 << " is less than " << str2 << '.' << std::endl; } else if(result == 0)…
RaymondTFR
  • 21
  • 3
-2
votes
3 answers

Why is strcmp returning false on the second run?

So right now I'm making a simplistic login system. The client enters the form data and the php handles it by checking a text file that goes like so: name pass name pass ...so on The php I'm using to process this is: $name = $_REQUEST['name']; $pass…
Andrew
  • 3,393
  • 4
  • 25
  • 43