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

strcmp() not returning what it should return

Basically I want to create a program that would have potential questions that might be in my upcoming exam in Digital Systems. #include #include int main() { char input1[600]; printf("What is the set of available…
YM_coding
  • 95
  • 8
-3
votes
3 answers

PHP strcmp not working

in my code I wanted to compare two strings and first I did it with === but later i tryed strcmp(). When i write: echo strcmp("test","test"); the result was 0. I also tried it with === echo ($subject === $empty) there i used my actual strings i…
-3
votes
1 answer

Converting a sentence into number by replacing digits into numbers. for instance ABCD to 2223 using strcmp

I am trying to create an application that can replace a character to a number. Let say A = 2 and F=3 if I write AFAF = 2323 should be the result, Please help. #include #include int main(){ char *test; char *result; int i,e =…
Test
  • 1
  • 1
-3
votes
3 answers

Segmentation. strcmp [C]

I have a file with format: [name][number][amount] number is taken as a string. and im using it in a strcmp. Problem is that i get a segmentation fault. I know that on most cases when strcmp signs segmentation fault it means that one of the…
FILIaS
  • 495
  • 4
  • 13
  • 26
-3
votes
1 answer

a given string is valid identifier or keyword in c++

This is a code of checking a given string is an identifier or a keyword. Here is the code: #include #include #include #include int main(){ int i = 0, flag = 0; char a[10][10] = {"int", "float",…
Rashed Sami
  • 141
  • 1
  • 4
  • 12
-3
votes
1 answer

I am not able to understand why the string compare results show up as false negative?

I am trying to understand when I specify the value of "strx" more than 10 characters, it always leads to strcmp showing up the value of -1 whereas when I specify value of strx less than 10 characters then strcmp shows the correct value of 0. My…
r0x0t
  • 19
  • 2
-3
votes
2 answers

stuck in array and string compare with strcmp c

stuck in array and string compare with strcmp c why ist it working?? compiler got stuck at if(strcmp(c,ch[i]) == 0){ #include #include #include int main(){ char ch[]="asdfghjkl"; char c; int…
FAHIM
  • 19
  • 1
  • 3
-3
votes
2 answers

strcmp doesn't return 0 for 2 exact files

i wrote a code with c, that reads from 2 files, and than i compare the 2 buffers with strcmp. for some strings, even when i read from the exact same file, strcmp returns !=0 anybody has an idea why? here is the relevent part from the…
Noam
  • 1
  • 1
-3
votes
3 answers

Comparing c strings to char using strcmp

I'm trying to count spaces using c strings. I can't use std strings. And on the line comparing the two chars I get the error 'invalid conversion from 'char' to 'const char*'. I understand that I need to compare two const chars* but I'm not sure…
cba1067950
  • 35
  • 1
  • 11
-3
votes
3 answers

Program in C++ to store ID and Password

This is a part of the program which I am required to make in Turbo C++; Here, if I give input of id as "PLAYNOW" and pass as "PASSWORD", variable p is storing the value 0 but i isn't storing. id variable is storing some junk number at the end of…
Chirag Jain
  • 11
  • 1
  • 3
-3
votes
1 answer

Problems with strcmp()

I'm not sure if it's strcmp or just the function itself but something isn't working. Here's the code in the main function. double findLow(const char* date, const Weather *data, int dataSize) { int i, o; for (i = 0; i < dataSize; i++) { …
-3
votes
1 answer

recursive function that return the first index where sub string contained in string

I need to make a recursive function, that gets two char arrays, and return the first index where "subStr" appear in the "str". Signature of the function: int strIndex(char str[], subStr[]); For example- for str="abcdebc" and subStr="bc", it will…
jaldk
  • 123
  • 1
  • 7
-3
votes
3 answers

what is wrong in this strcmp()?

I try to write simple C function with strcmp(). But I always get Segmentation fault (core dumped). What is wrong ? char *arr={"abcdefg"}; char *a = arr[1]; if(strcmp(a, 'b') == 0) { printf("it is b \n"); }
utarid
  • 1,642
  • 4
  • 24
  • 38
-3
votes
3 answers

C - How to scan an int only entered after symbol

I am having difficulty scanning from user input an integer (and storing it) only if printed directly after a !: char cmd[MAX_LINE/2 + 1]; if (strcmp(cmd, "history") == 0) history(hist, current); else if (strcmp(cmd, "!!") == 0) …
Sean
  • 1,283
  • 9
  • 27
  • 43
-3
votes
2 answers

CodeBlocks exe stops working

#include #include #include int main() { char string; printf("Hello\n"); printf("What would you like to do\n"); printf("Here are the options\n"); printf("s : How are you\n"); printf("c : What…