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

Strcmp — loop while no input

This program does the following: Scans a string of text char input[15]; Compares it to char password[ ] = "1sure"; Loops if the strings do not match. Terminates if the strings match. The program loops while the strings do not match. However, I…
grammer
  • 245
  • 3
  • 14
2
votes
4 answers

string Comparison

I want to compare two user input strings, but not able to do so... #include "stdafx.h" #include "iostream" #include "string" using namespace std; int _tmain(int argc, _TCHAR* argv0[]) { string my_string; string my_string2; cout<<"Enter…
user324463
  • 21
  • 1
  • 2
  • 3
2
votes
1 answer

C :How to compare two strings and find a word i need?

Its not complete, but in doesnt print anything char *fruit[] = {"rasberry", "cherry"}; char *veg[] = {"salad", "tomato"}; char word; printf("Print a veg or a fruit \n"); scanf("%s", &word); for (int i = 0; i < strcmp(fruit, veg ); i++) { if…
2
votes
1 answer

strcmpi code wont compile but strcmp will?

I have a question on why my code wont compile when i use strcmpi. I tested this same code with strcmp and that worked. Not sure why this does not work. here is the compile error i get : gcc -std=c99 strcmpi_test.c -o strcmpi_test strcmpi_test.c: In…
Max Powers
  • 1,119
  • 4
  • 23
  • 54
2
votes
5 answers

strcasecmp in C returns 156 instead of 0, any ideas why?

I have the following code: printf("num: %d\n", strcasecmp(buf, "h\n")); And I get the following results when I try plugging in different letters: a: -7 g: -1 i: 1 j: 2 h: 156 H: 156 Should strcasecmp not return 0 when buf is equal to H or h? Any…
hora
  • 3,661
  • 5
  • 25
  • 26
2
votes
2 answers

strcmp inside kernel module crash

I am trying to to detect the a outgoing packets in my kernel(Netfilter) module. I am using a strcmp function to achieve it. The kernel always crashes after loading my kernel module with strcmp function. I tried removing the strcmp function - loaded…
Gopi
  • 340
  • 4
  • 12
2
votes
4 answers

strcmp in C returns 1 instead of 0

I wrote the following code in C. #include #include int main(void) { char str1[4] = "abcd"; char str2[4] = "abcd"; printf("%d\n",strcmp(str1,str2)); return 0; } I expected the return value to be 0 (as I am…
2
votes
1 answer

C- Checking if the first character in the first comand line arguement contains a particular char

If the first character of the first argument == "-" then enter the if statement. The error I get is "passing argument 1 of ‘strcmp’ makes pointer from integer without a cast" I have also tried this with fgetc, written a little differently, but still…
JRX
  • 35
  • 1
  • 1
  • 7
2
votes
4 answers

Segmentation Fault using strcmp in C?

I was getting Segmentation Fault (core dump) error when I run the code. After using some printf statement I found out that there is an error in strcmp part, maybe it's because comparing a char with a string? How do I fix this? // this function…
Gavin Z.
  • 423
  • 2
  • 6
  • 16
2
votes
1 answer

Trying to compare strings in 2d array

So I have created a file that has a bunch of words in it, and my program is supposed to open the file, put all of the words in an array, and then compare the strings to see if any are exact matches. It opens the file and populates the array, but the…
nom_nutella
  • 171
  • 1
  • 10
2
votes
4 answers

How to create my own strcpy function?

I am trying to design a program in which I will create a 3 functions that resemble functions in the c-standard library (strlen,strcmp,strcpy). The first two I have gotten close to finishing, only the last one is the main problem. I am trying to…
Nathan Anderson
  • 21
  • 1
  • 1
  • 3
2
votes
4 answers

trouble with strcmp() in C

char* mystr = calloc(25, sizeof(char)); fgets(mystr, 25, stdin); // I enter "6 7 *" in here, without the quotes char* tok; tok = strtok(mystr, " "); while (tok != NULL) { if(strcmp(tok, "*") == 0) //It never meets this condition, but I…
user2889150
  • 23
  • 1
  • 3
2
votes
2 answers

Getting locale functions to work in glibc

I need to make some modifications to the the C standard library (glibc) in order to get some performance improvements. Specifically, I am going to be writing some specialized versions of some of the locale-dependent functions (which in glibc perform…
bddicken
  • 1,412
  • 1
  • 15
  • 16
2
votes
1 answer

replace word in string

this is an example of what I wanna do I have the following list [token] [token1] [token22] [token99] [8token] [3token3] and I wanna remove "[" and "]" then replace the word token with my own word. so I have the following code: char *getToken(char…
Sam Reina
  • 305
  • 1
  • 7
  • 22
2
votes
3 answers

strcmp behaviour in 32-bit and 64-bit systems

The following piece of code behaves differently in 32-bit and 64-bit operating systems. char *cat = "v,a"; if (strcmp(cat, ",") == 1) ... The above condition is true in 32-bit but false in 64-bit. I wonder why this is different? Both 32-bit and…
Nagaraju
  • 1,853
  • 2
  • 27
  • 46