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

What is the difference between these two strcmp() functions in PHP?

I'm having trouble understanding the difference between A) return strcmp($digest, $signature) == 0; and B) return strcmp($digest, $signature);
kylex
  • 14,178
  • 33
  • 114
  • 175
2
votes
2 answers

Comparing "" and "" in C

So I have the following test code: #include #include int main(int argc, char* argv[]){ int retVal = strcmp("", ""); printf("%d\n", retVal); return 0; } And for me, it always seems print out 0, i.e. "" and "" are always…
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
2
votes
1 answer

strcmp() Segmentation Fault in C

When I call this function, my code always breaks in the strcmp and returns a Segmentation Error with no more information provided. stop_t *getStop(char *name) { node_t *current = stop_list_head; stop_t *stop; while (current != NULL) { …
mcsmachado
  • 69
  • 6
2
votes
3 answers

Replicating the strcmp() function from string.h library

I am trying to replicate the strcmp() function from the string.h library and here is my code /** * string_compare - this function compares two strings pointed * by s1 and s2. Is a replica of the strcmp from the string.h library * @s1: The first…
Leuel Asfaw
  • 316
  • 1
  • 14
2
votes
1 answer

strcmp() function only works on first iteration C

I need to make a program that outputs all lines that contain a matching string "target" and sum up the number of matches along with the total cost, The problem is that the for loop stops at the first iteration regardless of there being a match I…
2
votes
3 answers

Mapping string to enum value

In my program, I have an enum that is used for indexing my array members. The reason is that it is much easier for me to understand which parameter I am accessing without knowing its index in the array enum param_enum { AA, AB, AC, …
TheBestPlayer
  • 324
  • 2
  • 13
2
votes
1 answer

Implement strcmp function in assembly 64 on linux

I am trying to implement strcmp which is a C function in assembly 64, here is my working code so far : global ft_strcmp section .text ft_strcmp: ;rax ft_strcmp(rdi, rsi) mov r12, 0 loop: mov r13b, [rdi + r12] …
Holy semicolon
  • 868
  • 1
  • 11
  • 27
2
votes
2 answers

I can't access a char as a string

I have the following bit of code: #include #include char values[] = {"abcde"}; int main(int argc, char *argv[]) { printf("%d\n", strcmp(&values[0], argv[1])); return EXIT_SUCCESS; } I compile and launch this way: $…
hacb
  • 175
  • 2
  • 10
2
votes
1 answer

How to exit the program immediately using strcmp() without printing the next line

The code itself is about typing a string and a character, and the program printing out what position the character is in the particular string. What I want to add to this is to exit the program immediately when the word "end" is inputed, using…
Learner_15
  • 399
  • 2
  • 11
2
votes
4 answers

strcmp usage to terminate file writing doesn't work

I write the following code to write a text. strcmp is supposed to terminate the file writing when the user type #. Still doesn't do it and i cannot exit the program #include #include int main() { FILE *fp; char c[100]; …
smksx
  • 47
  • 5
2
votes
1 answer

How to check if two strings are equal in fortran?

I want to check if two strings are equal and do some work. character(len = 50) :: x, y ,z x="amin" y="amin" if(llt(x, y)) then z=x end if I wrote this but it just checks first character in my string. How can i handle it?
cheamin
  • 33
  • 1
  • 6
2
votes
1 answer

Invalid read size of 1 strcmp

I'm trying to compare two name strings to return a matching struct. I've verified using gdb that both parameters passed to strcmp() are non-null with GDB. However, the program segfaults when I hit the strcmp() call. The output from valgrind…
Mokou
  • 63
  • 6
2
votes
1 answer

My program for checking the contents and length of the string is having some issues

I am not able to type the second input in the command prompt, it says my program has stopped. I am just trying my own code. I have added two functions to check for the length and contents of the string. And i can simply use strcmp(), but i am…
Sridhar
  • 21
  • 3
2
votes
3 answers

Side-effects: Is strcmp() a pure function

I am learning about side-effects and pure functions. I know that pure functions have no side-effects and their return value is the same for the same arguments. I would like to know whether the C function strcmp() is a pure function. I believe that…
ceno980
  • 2,003
  • 1
  • 19
  • 37
2
votes
0 answers

Using * in main() parameters for command-line calculator

I'm doing a homework problem which is to create a calculator using the argc and argv[] parameters in the main function. What we were supposed to do, and I did, was to create a char pointer array containing the operators I want to use. this is what I…
Omorio
  • 23
  • 5