我试图让一个程序让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序.我的代码看起来像这样:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!\nPlease enter a word or character:\n");
gets(input);
printf("I will now repeat this until you type it back to me.\n");
while (check != input)
{
printf("%s\n", input);
gets(check);
}
printf("Good bye!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是我不断打印输入字符串,即使用户输入(检查)与原始(输入)匹配.我比较错误吗?
Mys*_*ial 257
您不能(有用)使用!=
或比较字符串==
,您需要使用strcmp
:
while (strcmp(check,input) != 0)
Run Code Online (Sandbox Code Playgroud)
这样做的原因是因为!=
并且==
只会比较这些字符串的基址.不是字符串本身的内容.
Aus*_*oke 31
好的一些事情:gets
不安全,应该更换,fgets(input, sizeof(input), stdin)
以免你得到缓冲区溢出.
接下来,要比较字符串,必须使用strcmp
,其中返回值为0表示两个字符串匹配.使用相等运算符(即.!=
)比较两个字符串的地址,而不是它们中的个别字符串char
.
并且还要注意,虽然在这个例子中它不会引起问题,也可以fgets
将换行符存储'\n'
在缓冲区中; gets()
才不是.如果您将用户输入与fgets()
字符串文字进行比较,例如"abc"
它永远不会匹配(除非缓冲区太小,以至于它'\n'
不适合它).
编辑:再次被超级快速的神秘主义者击败.
thb*_*thb 11
欢迎来到指针的概念。几代新手程序员都发现这个概念难以捉摸,但如果你想成长为一名有能力的程序员,你最终必须掌握这个概念——而且,你已经在问正确的问题了。那挺好的。
你清楚什么是地址吗?看这个图:
---------- ----------
| 0x4000 | | 0x4004 |
| 1 | | 7 |
---------- ----------
Run Code Online (Sandbox Code Playgroud)
在图中,整数 1 存储在内存中的地址0x4000 处。为什么在一个地址?因为内存很大,可以存储很多整数,就像一个城市很大,可以容纳很多家庭一样。每个整数都存储在一个内存位置,就像每个家庭都住在一所房子里一样。每个内存位置都由一个地址来标识,就像每个房子都由一个地址来标识一样。
图中的两个框代表两个不同的内存位置。你可以把它们想象成房子。整数 1 驻留在地址 0x4000 的内存位置(例如“4000 Elm St.”)。整数 7 驻留在地址 0x4004 的内存位置(例如“4004 Elm St.”)。
您认为您的程序正在比较 1 和 7,但事实并非如此。它正在比较 0x4000 和 0x4004。那么当你遇到这种情况时会发生什么?
---------- ----------
| 0x4000 | | 0x4004 |
| 1 | | 1 |
---------- ----------
Run Code Online (Sandbox Code Playgroud)
这两个整数相同,但地址不同。您的程序会比较地址。
使用strcmp
.
这是在string.h
图书馆,非常受欢迎.strcmp
如果字符串相等则返回0.请参阅此内容以更好地解释strcmp
返回的内容.
基本上,你必须这样做:
while (strcmp(check,input) != 0)
Run Code Online (Sandbox Code Playgroud)
要么
while (!strcmp(check,input))
Run Code Online (Sandbox Code Playgroud)
要么
while (strcmp(check,input))
Run Code Online (Sandbox Code Playgroud)
您可以查看这个教程strcmp
.
小智 7
您无法像这样直接比较数组
array1==array2
Run Code Online (Sandbox Code Playgroud)
你应该比较char-by-char; 为此,您可以使用函数并返回布尔值(True:1,False:0).然后你可以在while循环的测试条件下使用它.
试试这个:
#include <stdio.h>
int checker(char input[],char check[]);
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!\nPlease enter a word or character:\n");
scanf("%s",input);
printf("I will now repeat this until you type it back to me.\n");
scanf("%s",check);
while (!checker(input,check))
{
printf("%s\n", input);
scanf("%s",check);
}
printf("Good bye!");
return 0;
}
int checker(char input[],char check[])
{
int i,result=1;
for(i=0; input[i]!='\0' || check[i]!='\0'; i++) {
if(input[i] != check[i]) {
result=0;
break;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
每当您尝试比较字符串时,请针对每个字符进行比较。为此,您可以使用名为 strcmp(input1,input2); 的内置字符串函数;你应该使用名为的头文件#include<string.h>
试试这个代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s[]="STACKOVERFLOW";
char s1[200];
printf("Enter the string to be checked\n");//enter the input string
scanf("%s",s1);
if(strcmp(s,s1)==0)//compare both the strings
{
printf("Both the Strings match\n");
}
else
{
printf("Entered String does not match\n");
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
409197 次 |
最近记录: |