I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this:
我正在尝试获得一个程序,让用户输入一个字或字符,存储它,然后打印它,直到用户再次输入它,退出程序。我的代码是这样的:
#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;
}
The problem is that I keep getting the printing of the input string, even when the input by the user (check) matches the original (input). Am I comparing the two incorrectly?
问题是,即使用户输入(检查)与原始(输入)匹配,我仍然得到输入字符串的打印。我是不是把这两者比较错了?
7 个解决方案
#1
210
You can't (usefully) compare strings using !=
or ==
, you need to use strcmp
:
您不能(有效地)使用!=或==来比较字符串,您需要使用strcmp:
while (strcmp(check,input) != 0)
The reason for this is because !=
and ==
will only compare the base addresses of those strings. Not the contents of the strings themselves.
这样做的原因是!=和=将只比较这些字符串的基本地址。不是弦本身的内容。
#2
28
Ok a few things: gets
is unsafe and should be replaced with fgets(input, sizeof(input), stdin)
so that you don't get a buffer overflow.
好,有几件事:get是不安全的,应该用fgets(input, sizeof(input), stdin)替换,这样就不会出现缓冲区溢出。
Next, to compare strings, you must use strcmp
, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=
) compares the address of the two strings, as opposed to the individual char
s inside them.
接下来,要比较字符串,必须使用strcmp,其中返回值为0表示两个字符串匹配。使用等式运算符。比较两个字符串的地址,而不是它们内部的字符。
And also note that, while in this example it won't cause a problem, fgets
stores the newline character, '\n'
in the buffers also; gets()
does not. If you compared the user input from fgets()
to a string literal such as "abc"
it would never match (unless the buffer was too small so that the '\n'
wouldn't fit in it).
还要注意,虽然在本例中它不会引起问题,但fgets也会在缓冲区中存储换行字符‘\n’;()不。如果您将来自fgets()的用户输入与字符串文本(如“abc”)进行比较,那么它将永远不会匹配(除非缓冲区太小,以至于‘\n’无法容纳它)。
EDIT: and beaten by the super fast Mysticial once again.
编辑:又一次被超快神秘术打败。
#3
6
You can't compare arrays directly like this
不能像这样直接比较数组
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
你应该对它们逐个进行比较;为此,您可以使用一个函数并返回一个布尔值(True:1, False:0)。然后您可以在while循环的测试条件中使用它。
Try this:
试试这个:
#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;
}
#4
4
Use strcmp
.
使用比较字符串。
This is in string.h
library, and is very popular. strcmp
return 0 if the strings are equal. See this for an better explanation of what strcmp
returns.
这是在字符串。h图书馆,很受欢迎。如果字符串相等,则strcmp返回0。有关strcmp返回的内容的更好解释,请参阅本文。
Basically, you have to do:
基本上,你必须这样做:
while (strcmp(check,input) != 0)
or
或
while (!strcmp(check,input))
or
或
while (strcmp(check,input))
You can check this, a tutorial on strcmp
.
您可以查看这篇关于strcmp的教程。
#5
1
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
当您试图比较字符串时,请将它们与每个字符进行比较。为此,可以使用名为strcmp的字符串函数(input1,input2);您应该使用名为#include的头文件
Try this code:
试试这段代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s[]="*";
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");
}
#6
0
Unfortunately you can't use strcmp
from <cstring>
because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp
:
不幸的是,您不能使用
int strcmp(char input[], char check[])
{
for (int i = 0;; i++)
{
if (input[i] == '\0' && check[i] == '\0')
{
break;
}
else if (input[i] == '\0' && check[i] != '\0')
{
return 1;
}
else if (input[i] != '\0' && check[i] == '\0')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
我希望这对你有好处。
#7
0
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("\nEnter different character of string to repeat: \n");
while(strcmp(s1,s2))
{
printf("%s\n",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
这是一个非常简单的解决方案,您可以在其中得到您想要的输出。
#1
210
You can't (usefully) compare strings using !=
or ==
, you need to use strcmp
:
您不能(有效地)使用!=或==来比较字符串,您需要使用strcmp:
while (strcmp(check,input) != 0)
The reason for this is because !=
and ==
will only compare the base addresses of those strings. Not the contents of the strings themselves.
这样做的原因是!=和=将只比较这些字符串的基本地址。不是弦本身的内容。
#2
28
Ok a few things: gets
is unsafe and should be replaced with fgets(input, sizeof(input), stdin)
so that you don't get a buffer overflow.
好,有几件事:get是不安全的,应该用fgets(input, sizeof(input), stdin)替换,这样就不会出现缓冲区溢出。
Next, to compare strings, you must use strcmp
, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=
) compares the address of the two strings, as opposed to the individual char
s inside them.
接下来,要比较字符串,必须使用strcmp,其中返回值为0表示两个字符串匹配。使用等式运算符。比较两个字符串的地址,而不是它们内部的字符。
And also note that, while in this example it won't cause a problem, fgets
stores the newline character, '\n'
in the buffers also; gets()
does not. If you compared the user input from fgets()
to a string literal such as "abc"
it would never match (unless the buffer was too small so that the '\n'
wouldn't fit in it).
还要注意,虽然在本例中它不会引起问题,但fgets也会在缓冲区中存储换行字符‘\n’;()不。如果您将来自fgets()的用户输入与字符串文本(如“abc”)进行比较,那么它将永远不会匹配(除非缓冲区太小,以至于‘\n’无法容纳它)。
EDIT: and beaten by the super fast Mysticial once again.
编辑:又一次被超快神秘术打败。
#3
6
You can't compare arrays directly like this
不能像这样直接比较数组
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
你应该对它们逐个进行比较;为此,您可以使用一个函数并返回一个布尔值(True:1, False:0)。然后您可以在while循环的测试条件中使用它。
Try this:
试试这个:
#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;
}
#4
4
Use strcmp
.
使用比较字符串。
This is in string.h
library, and is very popular. strcmp
return 0 if the strings are equal. See this for an better explanation of what strcmp
returns.
这是在字符串。h图书馆,很受欢迎。如果字符串相等,则strcmp返回0。有关strcmp返回的内容的更好解释,请参阅本文。
Basically, you have to do:
基本上,你必须这样做:
while (strcmp(check,input) != 0)
or
或
while (!strcmp(check,input))
or
或
while (strcmp(check,input))
You can check this, a tutorial on strcmp
.
您可以查看这篇关于strcmp的教程。
#5
1
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
当您试图比较字符串时,请将它们与每个字符进行比较。为此,可以使用名为strcmp的字符串函数(input1,input2);您应该使用名为#include的头文件
Try this code:
试试这段代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s[]="*";
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");
}
#6
0
Unfortunately you can't use strcmp
from <cstring>
because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp
:
不幸的是,您不能使用
int strcmp(char input[], char check[])
{
for (int i = 0;; i++)
{
if (input[i] == '\0' && check[i] == '\0')
{
break;
}
else if (input[i] == '\0' && check[i] != '\0')
{
return 1;
}
else if (input[i] != '\0' && check[i] == '\0')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
我希望这对你有好处。
#7
0
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("\nEnter different character of string to repeat: \n");
while(strcmp(s1,s2))
{
printf("%s\n",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
这是一个非常简单的解决方案,您可以在其中得到您想要的输出。