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

Why strcmp() works differently in code blocks (particularly in gcc) and other compilers?

I was just trying to execute this program in code blocks and i got an output of 0,1,-1 , in some other compiler i got result 0,4,-1 but according to the working of strcmp() , i should get 0,4,-32 , i am not able to understand why i am getting…
LocalHost
  • 910
  • 3
  • 8
  • 24
-2
votes
3 answers

What's wrong? C

I wanna compare an element of a string with a char, what's wrong? i get segmentation fault. i wanna go trough a string, copyng the part of string until an element of string, when the element=",", but i'm making something of wrong. passing argument…
-2
votes
1 answer

C - how to use strcmp for text files

I'm writing some code to read a text file on stdin using fgets which compares two adjacent lines from the 1st and 2nd all the way to the 2nd last and last for equality. My problem is that it works all the way till the final case. For example let's…
Calahan
  • 35
  • 6
-2
votes
2 answers

How to delete a singly linked list in C?

I'm working on a C code. I have a singly linked list, and i want to delete a node,which i don't know the position of it. My struct is this: struct list_node{ int num; char name[25]; int year; float money; struct list_node *next; }; typedef…
-2
votes
2 answers

how i can see if an array with arguments is null in c

i have this code : int main(int re, char *args[]){ int comp=1; int j=2; int count=0; //printf("%d",args[1]); while(comp!=0){ comp=strcmp(args[j],"null"); j++; count++; } } and i want to see how many string i have…
burrito
  • 143
  • 1
  • 2
  • 11
-2
votes
2 answers

Making strcmp() case insensitive without using strcmpi() (C++)

I am working a program that searches for a string (in this case a name) in a file. I wanted the program not to be case sensitive but strcmp is. I was thinking to convert bot the file and user input to lowercase. But that would be inefficient. Any…
O2Addict
  • 147
  • 11
-2
votes
1 answer

How does using strcmp() as a condition in an if() statement work?

The output of the below psuedocode(its psuedocode because it is not meant to be syntactically correct) produces "computers" strcpy(s1, "computer"); strcpy(s2, "science"); if(strcmp(s1, s2) < 0) { strcat(s1, s2); } else { strcat(s2,…
LiveToLearn
  • 163
  • 1
  • 2
  • 12
-2
votes
2 answers

How to compare char with a char linked list value?

So basically I have a linked list node that has a char variable called missed that takes in a single char earlier on in my program. I am just trying to compare that char to a hardwritten char 'a','b','c' or 'd' in order to increment a…
MidnightP
  • 105
  • 1
  • 4
  • 12
-2
votes
2 answers

If statement looks acceptable, but has error: expected expression before ‘)’ token

I have minimized my code to just what is necessary to reproduce this error. I have what I believe is a perfectly fine if statement, but gcc insists that it is not a valid statement. #define SOMECHAR * #include #include…
-2
votes
6 answers

What exactly is strcmp(String comparison) doing?

My following code for testing strcmp is as follows: char s1[10] = "racecar"; char *s2 = "raceCar"; //yes, a capital 'C' int diff; diff = strcmp(s1,s2); printf(" %d\n", diff); So I am confused on why the output is 32. What exactly is it…
CodeFreak
  • 90
  • 1
  • 2
  • 15
-2
votes
1 answer

comparing strings to pointers? Comparing strings in C

So this is my code for an assignment for school, and right now my problem is in my inputID function. Where the comment says "If the same!!!!!!!!!!!!!!!!!!!!!!!!!!!", I try to compare a string given by the user and a string stored in my array of…
-2
votes
3 answers

Is strncmp faster than strcmp

Is strcmp slower than strncmp as one can give pre-calculated string length to it, but strcmp does not receive such information ? I am writing an interpreter. I am aware that these functions are both optimized. I wonder what will be the better…
Imobilis
  • 1,475
  • 8
  • 29
-2
votes
1 answer

Output does not match strcmp() function in c

For comparing two strings by a strcmp() function i took one input string by fgets() and cin and another is given in function as default argument . But when i compare them by strcmp() funtion outputs does not match. char a[20]; int b; …
-2
votes
2 answers

Strcmp does not return equal on equal strings

I'm having trouble getting strcmp to return 0 for equal strings. This is how my code looks (the relevant part anyway): struct person{ char name[30]; time_t date; char destination[30]; char phone[30]; }; in the main i have: struct…
user1966576
  • 83
  • 1
  • 10
-2
votes
2 answers

Having problems with my recursive function

I don't understand why it stops right after it reaches the line " if(strcmp(next,str2) == 0) return; " for now I commented out the %.*s print line just so I can fully print the two strings that are being compared before it reaches the if statement…
Hispazn
  • 103
  • 3
  • 10