How to compare strings in an "if" statement? [duplicate] - c

This question already has answers here:
Comparing two strings in C? [duplicate]
(8 answers)
Closed 3 years ago.
I want to test and see if a variable of type "char" can compare with a regular string like "cheese" for a comparison like:
#include <stdio.h>
int main()
{
char favoriteDairyProduct[30];
scanf("%s",favoriteDairyProduct);
if(favoriteDairyProduct == "cheese")
{
printf("You like cheese too!");
}
else
{
printf("I like cheese more.");
}
return 0;
}
(What I actually want to do is much longer than this but this is the main part I'm stuck on.)
So how would one compare two strings in C?

You're looking for the function strcmp, or strncmp from string.h.
Since strings are just arrays, you need to compare each character, so this function will do that for you:
if (strcmp(favoriteDairyProduct, "cheese") == 0)
{
printf("You like cheese too!");
}
else
{
printf("I like cheese more.");
}
Further reading: strcmp at cplusplus.com

if(strcmp(aString, bString) == 0){
//strings are the same
}
godspeed

if(!strcmp(favoriteDairyProduct, "cheese"))
{
printf("You like cheese too!");
}
else
{
printf("I like cheese more.");
}

Have a look at the functions strcmp and strncmp.

You can't compare array of characters using == operator. You have to use string compare functions. Take a look at Strings (c-faq).
The standard library's strcmp function compares two strings, and returns 0 if they are identical, or a negative number if the first string is alphabetically "less than" the second string, or a positive number if the first string is "greater."

Related

How to compare two strings without case sensitivity? [duplicate]

This question already has answers here:
Case Insensitive String Comparison in C
(12 answers)
Closed 2 years ago.
I have a code:
int main()
{
printf("to exit the program type \"exit\":");
char s[40];
scanf("%s",s);
if(strcmp(s,"exit")==0)
{
exit(0);
}
else
{
int n;
printf("wrong");
}
return 0;
}
I want to when I enter "ExIt" it works.
how can i change that?
you can use tolower() in C, but it works for char, so maybe you need to itterate over s string and putting every char to lower case.
use stricmp instead of strcmp.
More letters to hit 30

If using string is not working

I want to make a stock calculator with C language, and it still in progress. I make it using Microsoft Visual Studio 2015.
When I input a string(example: "refrigerator") and I print it using printf, my computer can print it. But when that string put into an if it can't continue. The progress stop at:
scanf_s("%s",stock1,sizeof(stock1));
fflush(stdin);
After that when I input "refrigerator" the if statement is not coming out.
Here is my code:
int fan, refrigerator, lpg;
int menu;
char stock1[15], stock2[15];
int total1, total2, totalstock;
int check;
printf("Type the item that you want to add\n");
scanf_s("%s",stock1, sizeof(stock1));
fflush(stdin);
if (stock1 == "refrigerator")
{
printf("How many item would you like to add?\n");
scanf_s("%d", &total1);
fflush(stdin);
}
getchar();
return 0;
}
In C you should use the strcmp function to compare strings. For example, in your case you'd compare your string to "refrigerator" in the following manner:
if(strcmp(stock1, "refrigerator") == 0)
{
/* etc */
}
There are quite a few string functions available - you should probably familiarize yourself with them.
Best of luck.
Your problem is you are comparing a string literal with a char array that the wrong way of comparing string literals with a char array you must use
if(strcmp(stock,"refrigerator")==0)
instead of
stock1=="refrigerator"
strcmp return 0 if the string 1 is identical to string 2, >0 if string 1 is greater than string 2 and <0 if string1 is less than string2.
Since you're using C language, you must use strcmp function.
strcmp function returns 0 value if both strings have the same value.
Use it this way:
if(strcmp(string,"refrigerator") == 0)
{
/*string is equal to "refrigerator"*/
} else {
/*string is not equal to "refrigerator"*/
}
Don't forget to include string.h

why are these two strings not equal in c [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 6 years ago.
hey guys ok i wrote this code which would check if a get request is proper but at the last comparison it doesn't give me the expected result thank you in advance the problem is in the last if statement it return false when it should return true
#include <stdio.h>
#include <string.h>
int main()
{
char* string="GET /cats.html HTTP/1.1\r\n";
if(strncmp(string,"GET ",4)==0)
{
printf("hello");
if(string[4]=='/')
{
printf(",\n");
char* string1=strchr(string,'.');
string1=strchr(string1,' ');
printf("%s",string1);
if(string1!=NULL)
{
if(*string1==" HTTP/1.1\r\n")
{
printf("world\n");
}
}
}
}
}
The line:
if(*string1==" HTTP/1.1\r\n")
does not compare whether two strings are equal.
type of string1 is char*. Hence type of *string1 is char.
Hence, you are comparing a char with a char const*, which is far from what you expected.
Even using
if(string1==" HTTP/1.1\r\n")
will not give you the expected result since it will compare two pointers for equality, which will be false in this use case all the time.
What you need to use is the library function to compare two strings.
if( strcmp(string1, " HTTP/1.1\r\n") == 0)
Because you are not comparing strings, you are comparing pointers to strings.
The proper comparison would be:
if(!strcmp(string1," HTTP/1.1\r\n")) {
...
}

C cannot figure out if statement [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 7 years ago.
So I am trying to learn c however I can't figure out why this code won't run properly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char userName[25];
char myName[25] = "myName";
printf("Please enter your name: \n");
scanf("%s", userName);
if(userName == myName)
{
printf("Congratulations your name is myName!!!");
}
else
{
printf("Your name is %s how disappointing...", userName);
}
return 0;
}
The problem is that the if statement never seems to return true.
Can anyone help me with this?
This line is comparing the locations of the strings, which are different, since you are comparing two different strings.
if(userName == myName)
The correct test in C is to use a library function.
#include <string.h>
...
if(strcmp(userName,myName) == 0)
In C, you should use strcmp() to compare two strings.
So change this:
if(userName == myName)
to this:
if(strcmp(userName,myName) == 0)
You cannot compare strings like that. What you are doing is comparing the pointers to the respective stings(which are obviously different).
So,
userName == myName
won't work here. You may use string functions to compare the two strings.
The statement if(userName == myName) will not work the way you are expecting it to. Use the strcmp() function to compare two strings.
Use strcmp / strncmp declared in string.h to compare strings; in your program the values you are comparing (after conversion) are pointer values.

Multiple string comparison

I need to compare 3 strings to 1 string.
I am currently using the code below. The seatclass is entered by the user.
char first[10] = "FC";
char econ[10] = "EC";
char eandf[10]= "FC&EC";
if ((seatclass==first)||(seatclass==econ)||(seatclass==eandf))
{
printf("win");
}
else
{
printf("This is not a seatclass choose again");
getchar();
}
However when I enter FC or any other random value I always get
This is not a seatclass choose again
You need to use strcmp to compare in C.
Here is how you can do the comparison :
char first[10] = "FC";
char econ[10] = "EC";
char eandf[10]= "FC&EC";
if (strcmp(seatclass,first)==0 || strcmp(seatclass,econ)==0 || strcmp(seatclass,eandf)==0)
{
printf("win");
}
else
{
printf("This is not a seatclass choose again");
getchar();
}
NOTE : You can use '==' to compare string in C++
The response is that the "==" operator is comparing pointers in your case you should use the strncmp function (from string.h)
"==" will point to the memory addres in case of arrays and strings in C..instead you can use strcmp function.
To compare two strings, we can use the strcmp() or strncmp() functions.
Using a strcmp() function we can compare the hole string, Using the strncmp() function we can compare the some specific range of the string.
The both functions return 0 if the string is same, else it return -1 or 1

Resources