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

merging and sorting a text file in C

I am supoosed write a function that reads two text files line by line, compare them, delete the duplicates and but them into a third file in alphabetical order...I have been working on this for over a month and I am still stuck I have tried several…
user1385602
  • 1
  • 1
  • 1
  • 5
-2
votes
2 answers

How can i compare 2 teams with their winning percentage? in C

I get these errors with this code: in void teamEnter(){ [Error] expected identifier or '(' before string constant [Error] 'ans1' undeclared (first use in this function) [Error] 'ans2' undeclared (first use in this function) Actually I want to…
ICED OUT
  • 3
  • 3
-2
votes
1 answer

Why is the strcmp function returning 1?

The below code compares s[2] with the string "sw". Since I have assigned s[2] = "sw", shouldn't it return 1 when I use strcmp. But I am getting 0 as the value of i. #include #include int main () { char s[2] = "sw"; int i; …
-2
votes
1 answer

Comparing Multiple Strings Using || Logical Operator Is Not Working Properly in C language

I am trying to check if the user string input (after lowercasing user input) matches with required three strings i.e. rock or paper or scissor. If it doesn't match the requirement the system will print It is a wrong input. Otherwise, I'll do…
-2
votes
1 answer

Stuck in comparing "++X" with another string in C

I am trying to take a string as input and if it's equal to X++ or ++X, I would like to count it. for this purpose, I have written value code. Though it's working correctly for X++, for X-- it's not working. How can I solve the…
Md Nasir Ahmed
  • 23
  • 1
  • 10
-2
votes
2 answers

String comparison for constant string and a string element in C++ using strcmp

I am trying to convert III stored in string s in Roman numerals to 3. Here is a code snippet: int i = 0; int num = 0; while (i < s.size()){ if (strcmp(s[i], "I") == 0){ num = num + 1; i = i + 1; } else{ continue; …
bimbi
  • 70
  • 10
-2
votes
2 answers

How to save the index of a string in a vector?

Let's say I have a function int pozvec(vector vect) which takes as parameter a pre-filled vector of strings. I want to know at which position i there is the character "p", specifically "p". All of the strings are single-character, but the…
AndrewFNAF
  • 11
  • 4
-2
votes
1 answer

strcmp returning 10 even the strings are the same

for some reason when I compare two exact strings, it returns a number other than zero. I am not sure why is it doing this. Shown here: printf("%s %s %d\n",target,temp->variableName,strcmp(temp->variableName, target)); Result:"B B 10" <- when I…
www.com
  • 1
  • 4
-2
votes
1 answer

Segmentation fault c strcmp at last comparaison

I'm currently trying to compare each String from a char** with another String. In order to do so I tried to use strcmp but I get a Segmentation Fault. Then i tried to use a hand-made strcmp, and by testing I have been able to spot that my seg. fault…
kreek
  • 3
  • 1
-2
votes
2 answers

My if statement is doing the opposite of what I want it to do

I have a program that asks the user to input a word, and each word they enter is added to a linked list. When the user enters "END" the program is supposed to list all the nodes. My problem is that the program only adds the word "END" into the list,…
Jake Jackson
  • 1,055
  • 1
  • 12
  • 34
-2
votes
1 answer

String compare is not working as expected in c language

#include #include int main() { char str1[50]="TEST sun raised"; char str2[4][90]={"sun","in"}; char delim[] = " "; char *ptr=strtok(str1,delim); while (ptr!=NULL) { int i=0; for (i=0; i<4; i++) { …
-2
votes
2 answers

How to compare more than one string in if statement in C?

In C language, I have created a program with a virtual bot so that when you start the program it introduces itself and I've given it the name Nick. When the user launches the program, it asks for the user's name and I want to write a code that if…
Nick
  • 3
  • 1
-2
votes
1 answer

strcmp cannot convert 'char**' to 'const char*' for argument '2' to 'int strcmp(const char*, const char*)'

Porting code for arduino nano to esp8266 // list of fields that must be quoted in JSON convertion char RFLINK_FIELD_NAME_CMD[] = "CMD"; char RFLINK_FIELD_NAME_BAT[] = "BAT"; char RFLINK_FIELD_NAME_SMOKEALERT[] =…
francisp
  • 19
  • 2
-2
votes
3 answers

SIGSEV on strcmp of memset string

In the following program, i am expecting the for loop to stop after 3 elements. But it keeps on going indefinitely and fails later on with a coredump. is malloc() needed forchar*[]` would strcmp fail if i memset to…
anurag86
  • 1,635
  • 1
  • 16
  • 31
-2
votes
2 answers

Why is strcmp not returning 0

In this mini version of my program I am asking for user input. When the input is 'quit' or 'exit' I want the program to exit the while loop. The strcmp function doesn't seem to be working as I expect. I've spent some time looking for answers but…