如何在条件下检查字符串? [英] How to check string in if condition?
问题描述
当我需要检查用户的字符串时,为什么这个if条件不起作用?
#include < stdio.h >
main()
{
char a;
printf( 输入名称\ n);
scanf( %d,& a);
if (a == ' xyz')
{
printf( 输入xyz< /跨度>);
}
if (a == ' abc')
{
printf( 你输入abc);
}
}
我也试试这个,但即使不工作
#include< stdio.h>
main()
{
char a;
printf(输入名称\ n);
scanf(%c,& a);
if(a ==''xyz'')
{
printf(你输入xyz);
}
if(a ==''abc'')
{
printf(你输入abc);
}
}
Jonathon有正确的想法,但即使你已经读过teh的字符串了用户相关地说,你不能只使用==来比较两个字符串 - 它只会将变量字符串地址与固定的字符串地址进行比较,并且总是无法匹配。
您需要查看使用 strcmp [ ^ ]而不是比较字符串内容:
if (strcmp(a, xyz)== 0 )
{
...
}
Quote:char a;
printf(输入名称\\ \\ n);
scanf(%c,& a); 更改为
char a [ 256 跨度>];
printf( 输入名称\ n);
fgets(a, 256 ,stdin);
引用:if(a ==''xyz'')
{
printf(你输入xyz );
}
if(a ==''abc'')
{
printf(你输入abc);
}改为:
if (strcmp(a, xyz)== 0 )
{
printf( 您输入了xyz \ n 跨度>);
}
else if (strcmp(a, abc)== 0 )
{
printf( 您输入了abc \ n);
}
您也可以简单地写一下:
printf( 您输入了%s \ n,a);
注意你必须包含string.h
才能使用strcmp
。
[更新]
你是对的,我忘记了恼人的换行不被fgets丢弃。尝试:
#include < stdio.h >
#include < string.h >
int main (){
char a [ 256 ] = { 0 跨度>};
int len;
printf( 输入名称\ n);
if (fgets(a, 256 ,stdin))
// 用字符串终止符替换可能'not-abandoned'的换行符
len = strlen(a);
if (len> 0&& a [len- 1 ] == < span class =code-string>' \ n')
a [len- 1 ] = ' \ 0';
if (strcmp(a, xyz)== 0 )
{
printf( 您输入了xyz \ n);
}
else if (strcmp(a, abc)== 0 )
{
printf( 您输入了abc \ n);
}
printf( '%s'\ n,a );
return 0 ;
}
[/更新]
>
尝试:
scanf(%s,& a)
另外,你需要将a的定义更改为合适大小的字符数组。
Why this if condition is not working when i need to check string from user?
#include <stdio.h> main() { char a; printf("Enter the Name\n"); scanf("%d",&a); if(a=='xyz') { printf("You enter xyz"); } if(a=='abc') { printf("You enter abc"); } }
I also try this one but even not working
#include <stdio.h> main() { char a; printf("Enter the Name\n"); scanf("%c",&a); if(a==''xyz'') { printf("You enter xyz"); } if(a==''abc'') { printf("You enter abc"); } }解决方案Jonathon has the right idea, but even once you have read the string from teh user correectly, you can''t just use "==" to compare two strings - all it will do is compare a variable string address with a fixed string address and will always fail to match.
You need to look at using strcmp[^] instead to compare the string content:
if (strcmp(a, "xyz") == 0) { ... }
Quote:char a;
printf("Enter the Name\n");
scanf("%c",&a);change to
char a[256]; printf("Enter the name\n"); fgets(a, 256, stdin);
Quote:if(a==''xyz'')
{
printf("You enter xyz");
}
if(a==''abc'')
{
printf("You enter abc");
}change to:
if (strcmp(a, "xyz") == 0) { printf("You entered xyz\n"); } else if (strcmp(a, "abc")==0) { printf("You entered abc\n"); }
You may also simply write:
printf("You entered %s\n", a);
Note you must includestring.h
in order to usestrcmp
.
[UPDATE]
You are right, I forgot the "annoying newline NOT discarded by fgets". Try:
#include <stdio.h> #include <string.h> int main() { char a[256]={0}; int len; printf("Enter the Name\n"); if ( fgets(a,256,stdin) ) // replace the possibly 'not-discarded' newline with string terminator len = strlen(a); if (len>0 && a[len-1]=='\n') a[len-1]='\0'; if (strcmp(a, "xyz") == 0) { printf("You entered xyz\n"); } else if (strcmp(a, "abc")==0) { printf("You entered abc\n"); } printf("'%s'\n", a); return 0; }
[/UPDATE]
Try:
scanf("%s", &a)
Plus, you''ll need to change the definition of a to a character array of a suitable size.
这篇关于如何在条件下检查字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文