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
vote
2 answers

php string compare

I'm trying to compare a specific part of a url to get a list of the endings (which are resource/'location' where location is either state initials or a string). I'm using this to populate a drop down menu. This works well with the 2 letter states,…
DatabaseDiver
  • 73
  • 1
  • 2
  • 10
1
vote
2 answers

Typecast for qsort function pointer

#include #include #include #include static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to…
user678392
  • 1,981
  • 3
  • 28
  • 50
1
vote
1 answer

How to compare a "string user input" with a "list of string in a array" inside a for loop in C

I am new to C programming language and doing some hands on. I wanted to compare a string user input with a list of strings in a array using a for loop. My code looks something like this :- #include #include void…
1
vote
3 answers

How avoid I a comparation with NULL in strcmp?

I'm tring to make this program work, but after giving it a lot of turns, I decided is time to ask. No matter what I do the result is always a segmentation fault and I pretty much sure that is for the strcmp. Can you help me please? #include…
1
vote
1 answer

How can I use strcmp properly when I have more than two strings?

So the aim of my code is to get an input, add the digits of the input, then add the sum to the input and do this until the number is over 1000. There is no problem with calculation however at the beginning of my code, I ask a yes or no question and…
ezgi
  • 13
  • 3
1
vote
3 answers

Anything wrong spec-wise with this string comparison optimization?

Assuming no string less than 4 bytes is ever passed, is there anything wrong with this optimization? And yes it is a significant speedup on the machines I've tested it on when comparing mostly dissimilar strings. #define STRCMP(a, b) (…
Edwin Skeevers
  • 309
  • 1
  • 8
1
vote
3 answers

Character Pointers (allotted by new)

I wrote the following code: char *pch=new char[12]; char *f=new char[42]; char *lab=new char[20]; char *mne=new char[10]; char *add=new char[10]; If initially I want these arrays to be null, can't I do this: *lab="\0"; *mne="\0"; and so…
AvinashK
  • 3,309
  • 8
  • 43
  • 94
1
vote
2 answers

Compare arrays with different sizes with strcmp()

I'm currently learning C and I have this question... char name[6] = "Pedro"; char test[25]; printf("Insert a name: "); fgets(test, 25, stdin); if(!strcmp(name, test)) puts("They are equal..."); During the execution, strcmp doesn't return 0…
Pedro Luiz
  • 33
  • 3
1
vote
1 answer

Strcmp 1-D array vs 2-D array in c

When using strcmp function (in the string.h library) , passing a 2-D character array as a parameter comparison , there is a need to add the ampersand symbol like &mainlist[i][0] otherwise like mainlist[i][0] there's an error warning: passing…
Steve Bob
  • 5
  • 5
1
vote
1 answer

Checking if table contains any element of a specific column in another table

I want to check if a table column contains any of the elements in another table. Here's the tables: list1 = ["Hey";"H";"Say"]; list2 = ["Ey";"H";"S"]; Table1 = table(list1); Table2 = table(list2); Now, to check if Table2 contains any of the items…
1
vote
2 answers

How to check if the string is already in an array of structures?

How to check if the string is already in an array of structures? The array is inside a structure and I don't know how to compare arrays inside the structure in C. #include #include struct song { char title[30]; char…
1
vote
1 answer

How to detect duplicate string using strcmp()

#include #include #include struct stud { char nam[20]; int num; char letter[5]; }; int main() { struct stud s[5]; int i, j; for(i = 0; i < 5; i++){ printf("Enter the name of student #%d:…
User234567
  • 93
  • 7
1
vote
1 answer

Valgrind error: Invalid read of size 1 disappears when switching to different variable declaration

I have a while loop which reads stdin until it gets an empty line. It looks like this: while (strcmp((fgets(line, BUFFER_SIZE - 1, stdin)), "\n") != 0) { process_line(line, option); printf("%s", line); } When I have line initialized…
1
vote
1 answer

Comparision of strings using strcmp and ==

I am comparing two strings a and b in the code below. strcmp() returns 0 saying that the strings are equal. But the comparision operator == returns false. Is it due to the difference in memory allocated to these strings? If so, why doesn't a…
1
vote
1 answer

strcmp() doesn't seem to work when used as a condition in a "for" loop - C language

I tried to write a function that has as an input a string called name as well as other inputs ( irrelevent for the purpose of this question). One of the things required is to do "something" after checking if name (input of function) corresponds to…
Atmane Lee
  • 31
  • 6