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

Comparing two files for matching lines in C Programming

I am writing a program to compare two files. If matching lines occur then the program will continue to do some task. My second file has only one line and the first file has several lines Contents of File_1 apple is red oranges are orange banana is…
John P Raj
  • 33
  • 6
2
votes
3 answers

strcmp returns wrong value

Can someone explain me why strcmp returns the same value even if passwords are correct/incorrect? I define valid password just below include section and checking it with entered one at the end of my program. Here's my code: #include…
Raven
  • 57
  • 5
2
votes
0 answers

strcmp in Intel x86 with cmpsb

I want to code a strcmp in nasm x86. The main problem is when it has to return a negative value. I've tried the following, but in many cases it just returns a wrong value. I'm calling it from C, like: strcmp("abc", "abd") and I expect an int to be…
cahenepudi
  • 31
  • 3
2
votes
0 answers

When I dup2 STDOUT it takes all its contents

I am making a shell (works well). However, right now I am trying in implement output redirection. cat test1.txt > text2.txt. If I run commands without redirection, it works perfectly. So what am I missing in my redirection output…
Paul
  • 127
  • 1
  • 1
  • 7
2
votes
2 answers

Trouble using strcmp in c code

I've used strcmp before and it worked as expected, but it's not working for me in my current code. I'm reading a .csv file with the names of a bunch of famous people. "Mark Zuckerberg" is the key name that triggers things that my code will…
Brian.H
  • 73
  • 1
  • 7
2
votes
1 answer

Understanding strcmp in assembly

otool on Mac gives this assembly for a strcmp rep cmpsb %es:(%edi), (%esi) movl $__mh_bundle_header, %eax je 0xe0eb Some of this makes sense: edi and esi are char pointers to the strings to be compared. cmpsb compares the first character…
Dim St Thomas
  • 103
  • 1
  • 7
2
votes
3 answers

Can I use strcmp without pointers?

I have to make a program which takes a file of DNA sequences and a DNA subsequence from command arguments and find each time the subsequence and how many times it occurs. I'm having troubles with strcmp in line 36 and 42. Currently the way I have it…
popy2308
  • 33
  • 3
2
votes
2 answers

php - 2 identical string showing different lengths

I am having a problem comparing 2 identical strings. The first string is retrieved from a database and the other hard coded. The string is { "name":"John", "age":30, "car":null }. I've first run this code and the database string has a length of 79…
Peter Griffin
  • 300
  • 5
  • 18
2
votes
5 answers

Checking the first letter of a string in c

I am writing a very simple function in C to check if a string is an absolute path or relative path. No matter what I try it is always returning false. Here is what I have tried: int isAbsolute(char *str){ if(strcmp(str,"/")){ return 1; …
CodySig
  • 174
  • 1
  • 4
  • 15
2
votes
1 answer

comparing strings in two tables matlab

I know matlab can conveniently get all rows from a table that have the string (in this case) desired_a in the 'a' column like so: refs_found = refs(strcmp(refs.a,desired_a),:); However, I want to do this in the case that desired_a is not a string,…
Roger That
  • 95
  • 1
  • 12
2
votes
5 answers

Can't understand small part of strcmp function

I'm reading a book in C and have seen these two strcmp algorithm. I have learned my self how the usel for loop works. But these two for loop are new for me. I don't understand these parts for (i = 0; s[i] == t[i]; i++) It have no length instead…
S landy
  • 33
  • 3
2
votes
1 answer

MATLAB: struct.name type and strcmp

I have the following code: fonts = dir('fonts') strcmp('BELL.TTF',fonts.name) where dir('fonts') returns a 33x1 struct where each entry has name (string), date, and a few other things. I can't figure out what type fonts.name is (if it's a cell…
Hanmyo
  • 543
  • 4
  • 8
  • 18
2
votes
2 answers

Working with pointers: program that checks if strings are in lexicographical order

I am trying to make a program which checks if a list of Strings are in lexicographical order or not. If it is in order, the program should return 1, if not 0. To test the program I introduced the strings AA, BB and CC which obviously should be in…
Mark
  • 209
  • 3
  • 8
2
votes
3 answers

C: Working with strcmp

As a beginner I've been playing around with some of the functions of the library string.h and have some issues regarding the function strcmp. I wrote the program comparing two string. If they are equal it returns YES and NO otherwise. #include…
asd
  • 1,017
  • 3
  • 15
  • 21
2
votes
1 answer

Check if input matches word in C

I am building a program, which needs to read input from user in form of command such as 'command 12' where command is specific word and needs to be stored in separate variable, number after it needs to be in its variable too. I created variable in…
Pernick
  • 33
  • 2
  • 9