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
Related
This question already has answers here:
Returning an array using C
(8 answers)
How do I trim leading/trailing whitespace in a standard way?
(40 answers)
Closed 5 months ago.
char stripper(char x[]){
char stripped[20];
int c=0;
for(int i=0; i<strlen(x);i++){
if(x[i]==' '){
continue;
}
else{
stripped[c]=x[i];
c++;
}
}
return stripped;
}
this is the function i've created to return a string/sentence without whitespaces. using this function as a void function and simply printing the stripped string works, but this iteration of the code returns (null). i am somewhat of a newbie to C so simple solutions to solve this problem would be appreciated. Thanks!
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")) {
...
}
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.
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 7 years ago.
I have an apparently easy issue but I just cannot get what am I doing wrong.
I've got a code which will test a 60 character entered text in console and if in that text the "terrorist" word appears it will prompt the message "suspect text" and when that word does not appear it would show "nothing suspect".
The text entering "mode" should finish when the word "done" is entered. This seems to be my problem because my while loop just does not want to end.
Any hints?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int start_with (char *sir1, char *sir2)
{
int i,j,k;
int len_sir2=strlen(sir2);
char sir3[60]="";
for (i=0;i<=len_sir2;i++)
{
sir3[i]=sir1[i];
}
k=(strcmp(sir2,sir3)) ? 0:1;
return k;
}
int main()
{
char *txt1;
char sir1[60]="", sir2[60]="terorist", sir_test[60]="done";
int i,j,lensir1, contor=0,buf_de_la=0, buf_la;
while (sir1!=sir_test)
{
printf("Enter desired text and press ENTER \n");
gets(sir1);
printf("\n");
buf_la=strlen(sir1);
char *txt1="Nothing suspect";
while (buf_de_la<buf_la-7)
{
char sirbuf[60]="";
j=buf_de_la;
for (i=0;i<=7;i++)
{
sirbuf[i]=sir1[j];
j=j+1;
}
if (start_with(sirbuf,sir2)==1)
{
txt1="SUSPECT text entered!";
break;
}
buf_de_la=buf_de_la+1;
}
printf("%s\n",txt1);
getch();
system("cls");
}
return 0;
}
You should compare C-style strings by using strcmp, like this
while (strcmp(sir1, sir_test) != 0)
This happens because when you do sir1!=sir_test, what you are actually testing is if both character arrays are pointing to the same address in memory, which is not true, since they are two different character arrays located at two different memory addresses.
Instead, what you want to do is compare the characters contained in each variables memory space, and that's what strcmp does: it compares character by character until a null terminating character is found.
Instead of while (sir1!=sir_test), try this: while (strcmp(sir1,sir_test)). strcmp, strcmpi etc. are the proper functions to be used for string comparisons. They are declared under the string.h header file.
The issue is with your comparison while (sir1!=sir_test) which is wrong. It actually compares the addresses of the two strings. You can use strcmp or various other forms of the same function for comparing strings.
A small change which could make your code working is shown below..
do{
printf("Enter desired text and press ENTER \n");
.
.
.
system("cls");
}
while (strcmp(sir1,sir_test));
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."