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

I do not understand strcmp results

this is my implementation of strcmp , #include #include int ft_strcmp(const char *s1, const char *s2) { while (*s1 == *s2) { if (*s1 == '\0') return (0); s1++; …
user3540997
  • 49
  • 1
  • 1
  • 9
3
votes
5 answers

How to compare more than 2 strings in C?

I know there is the strcmp but it just let me compare two strings, And I need to compare a lot of them This one doesn't work: if(strcmp (resposta, "S" || "s" || "N" || "n")== 0) printf("Resposta = S"); else printf("Resposta !=…
Dannark
  • 716
  • 6
  • 12
3
votes
3 answers

scanf and strcmp with c string

I found a nice example of how to use strcmp, but it's only working with fgets(), and i need to make it work with scanf. So, here's the code: int main(void) { char fruit[] = "apple\n"; char ans[80]; do { printf ("Guess my favorite fruit?…
Daniel Mendonça
  • 391
  • 1
  • 3
  • 14
3
votes
4 answers

String compare isn't working correctly in C

I can't figure out why my string compare isn't comparing correctly. This is for C. It's reading from a file that is set up like this: 1 - ls 2 - cd 3 - history If I type !c it's suppose to grab the…
user2318083
  • 567
  • 1
  • 8
  • 27
3
votes
6 answers

Compare two chars

Why won't this work? I want to compare two chars. //Login. char myName = 'name'; //Login Temp. char nameTemp[10]; again: printf("Name?\t"); scanf("%c", *nameTemp); if (strcmp(myName, nameTemp) == 0) { …
Funktiona
  • 113
  • 2
  • 11
3
votes
2 answers

compare const char * with strcmp

I compare a const char * to a string and for some reason it always succeeds. if (std::strcmp(t->detectColor->name, "ghghjg") != 0) { printf("XXXXXXXXXXX\n"); // check if it was allready a sequencer if…
clankill3r
  • 9,146
  • 20
  • 70
  • 126
3
votes
3 answers

strcmp function not working properly

I have a delete function on array of structures books. I'm passing it an array of records, author of book and name of book and size of the list. Now here given that list[0].author and list[5].author and author all are equal to "Dan Brown" (same…
user2675010
3
votes
6 answers

Logic challenge: sorting arrays alphabetically in C

I'm new to programming, currently learning C. I've been working at this problem for a week now, and I just can't seem to get the logic straight. This is straight from the book I'm using: Build a program that uses an array of strings to store the…
austin robinson
  • 51
  • 1
  • 1
  • 6
3
votes
3 answers

Seg Fault using strcmp with * char

I have this struct typedef struct no { char command[MAX_COMMAND_LINE_SIZE]; struct no * prox; } lista; lista *listaCommand = NULL; and I'm filling listaCommand with a simple function that seems to work ok, as I can read the values without…
Dante003
  • 55
  • 4
3
votes
4 answers

Why the returns of strcmp is different?

Here is the C code and I compiled with gcc char *a="a"; char *d="d"; printf("%d\n", strcmp("a", "d")); printf("%d\n", strcmp(a, "d")); printf("%d\n", strcmp(a, d)); When I compiled with -O the output is -1 -3 -1 When I compiled without -O then…
solomon_wzs
  • 1,711
  • 5
  • 16
  • 29
3
votes
4 answers

C - strtok and strcmp

I am having a bit of trouble using strtok with strcmp. //Handles the header sent by the browser char* handleHeader(char *header){ //Method given by browser (will only take GET, POST, and HEAD) char *method,*path, *httpVer; …
Señor Reginold Francis
  • 16,318
  • 16
  • 57
  • 73
3
votes
6 answers

strcmp returning unexpected results

I thought strcmp was supposed to return a positive number if the first string was larger than the second string. But this program #include #include int main() { char A[] = "A"; char Aumlaut[] = "Ä"; printf("%i\n",…
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
2
votes
2 answers

strcmp conversion from ‘char’ to ‘const char*’

I'm having a problem with strcmp. This is my code. while (strcmp("m",wood) !=0 || strcmp("j",wood) !=0 || strcmp("o",wood) !=0){ cout << "(m for mahogany, o for oak, or p for pine): "; cin >> wood; } And this is my error: dining.cpp: In member…
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
2
votes
2 answers

qsort segmentation fault

So I'm working on a program where the function reads in from stdio, and keeps reading in characters in chunks of n characters. So far I've gotten it so that everything is stored in a character array called buffer. For the next step, I need to sort…
user1161080
  • 165
  • 1
  • 5
  • 11
2
votes
4 answers

segmentation fault with strcmp?

I am trying to understand why my code crashes. I have an array of structs which look like this: typedef struct contact { char cFirstName[10]; char cLastName[10]; char cTelphone[12]; } address ; // end type In the code I initialize the…
oz123
  • 27,559
  • 27
  • 125
  • 187