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

Cannot read a file and get the outcome

I want to read the file and match 2 words first i connect with a socket get the input write it to a file then i wanna read that file and get the match of 2 words and do something afterword but seems like it doesn't work So here is the…
f00
  • 1
  • 1
-4
votes
5 answers

C/C++ strcmp cannot convert argument 1 from 'char' to 'const char *'

So I'm passing a char Array(En) that consists a few words and I'm trying to sort alphabetically. Unfortunately, my compiler explodes with " int strcmp(const char *,const char *)' : cannot convert argument 1 from 'char' to 'const char *" and I'm…
-4
votes
6 answers

C programming language: Behavior of strcmp(str1, str2)

In C, I have a character array: d[20] Which is assigned the value 'if' with a null termination character: d[0]='i' d[1]='f' d[2]='\0' Should the value of strcmp(d,"if") be 0? Why? I expect strcmp to return a value of 0. When I run it, I get…
Hick
  • 35,524
  • 46
  • 151
  • 243
-5
votes
1 answer

why strcmp function is not giving correct comparison result in c program

So below is my source code in c in which i have used strcmp function to compare two strings #include #include int main() { unsigned char pass[100]="Try to hack me"; unsigned char input[100]; printf("Enter the secret string:…
Swapnil
  • 9
  • 4
-5
votes
2 answers

STRCMP on numeric strings

so I have 3 variables: char fromAge[4]; char toAge[4]; char age[4]; They can all have a number between 18 and 100 (include 18 and 100). When I give them the follwing values, the following statement is wrong for some reason: fromAge[4] =…
Idan
  • 227
  • 3
  • 10
-5
votes
1 answer

strcmp and strcpy is broken. I lost a word when i try to order them alphabetical in C

right now I'm working on a bigger project. It's a long story, Santa Claus needs some help he has some cities and some toys to send. I need a function to order my cities alphabetical and i made one but, it worked fine for 9 tests and at the last one…
-5
votes
1 answer

in c, if i have char *const* pointer, how do i access each and every element?

this is for the qsort comparison function, int cmp(const void *p, const void *q) { char *const* pp = p; char *const* qq = q; ... } i want to compare the elements of the two strings. i tried (*pp)[i] since *pp is p, but it doesnt seem…
-5
votes
1 answer

trying to use strcmp in if function for counting anagrams in a sentence

hii guys i need a serious help i m trying to write a code for finding anagrams in input sentence but when the if function is getting strcmp it stops and its not accepting the condition. any body know why is that happening Basically my code supposed…
BIG.E
  • 1
-5
votes
1 answer

Why do I get a segmentation fault with strcmp

I believe it is because of the strcmp(). I tried it multiple ways so far and this is just the latest. My objective is to get the index of the array so I can go on to a switch statement to execute code. Any help would be appreciated, although I'm…
Kyle
  • 27
  • 1
  • 8
-5
votes
2 answers

Infinite loops in C

I'm pretty sure this this piece of code gives me an infinite loop, (I have left if for a very long time and nothing happens), and I've been starring at this for 2 days now and i don't have a clue why it keeps looping. Any ideas? int r = 0; …
J.doo
  • 11
  • 3
-5
votes
3 answers

Error while looping for strcmp

I want to scan for a string and check if it can be found in a compiled struct and return some values of this. if not the relative message. I have the following code but I get errors. The error is at case 4. The rest of the cases 5 and 6 I am still…
falsobuio
  • 93
  • 1
  • 10
-5
votes
2 answers

strcmp in a function in c++

I am just trying to learn how to use c++ and one thing that I am trying to make is a yes or no choice "choose y to continue choose n to exit" kinda thing I know other ways of doing this other then this method but I am trying to learn the concept of…
-5
votes
3 answers

Invalid use of void expression in strcmp

I am looking to check if two string are permutations of each other. I am using the following code : #include #include #include using namespace std; void sort(char *str) { char temp; for(int…
Shivam Bhalla
  • 1,879
  • 5
  • 35
  • 63
-6
votes
2 answers

strcmp refuses to work

I've tried everything, yet strcmp (as well as strncmp) always give a value other than 0, both with and without using pointers, as well as inside and outside functions. What should I do? The point of the program is to create a student data system for…
Tasos500
  • 107
  • 2
  • 11
-6
votes
5 answers

strncmp don't work as it should

I'm having problems using strncmp. As I read, theorically strncmp should return 0 if the characters compared of two strings are equal; however, when I do the comparation, the code misbehaves and makes a false positive (Not being equal the…
DaniWein
  • 43
  • 1
  • 5
1 2 3
63
64