无法使用从 fgets 输入的字符串搜索字符
Unable to search character with string input from fgets
我正在编写一个程序,使用 strchr
查找字符串中特定字符的位置。当使用 fgets
从用户输入字符串时,程序无法正常执行。
然而,当使用 gets
时一切正常。
使用 gets()
的代码:
#include <stdio.h>
#include <string.h>
int main() {
char let;
char input[50];
char *ptr;
printf("what is the string that you would like to check?\n");
gets(input);
printf("what is the character that you would like to find?\n");
let = getchar();
printf("in string \"%s\"...\n", input);
ptr = strchr(input, let);
while (ptr != NULL) {
printf("the letter %c was found at character %d\n", let, ptr - input + 1);
ptr = strchr(ptr + 1, let);
}
return 0;
}
输出:
what is the string that you would like to check?
what is the character that you would like to find?
in string "why is the world when wondering"...
the letter w was found at character 1
the letter w was found at character 12
the letter w was found at character 18
the letter w was found at character 23
无效的代码usgin fgets()
:
#include <stdio.h>
#include <string.h>
int main() {
char let;
char input[50];
char *ptr;
printf("what is the string that you would like to check?\n");
fgets(input, 16, stdin);
printf("what is the character that you would like to find?\n");
let = getchar();
printf("in string \"%s\"...\n", input);
ptr = strchr(input, let);
while (ptr != NULL) {
printf("the character is found at %d \n", ptr - input + 1);
ptr = strchr(ptr + 1, let);
}
return 0;
}
输出:
what is the string that you would like to check?
what is the character that you would like to find?
in string "abcdefghijklmno"...
改变
fgets(input, 16, stdin)
到
fgets(input, sizeof(input), stdin)
当您将 16
的参数传递给 fgets()
时,您是在指示它读取 不超过 15 个字符 。为什么?
来自 fgets()
联机帮助页:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.
如果您提供多于size -1
个字符,剩余的字符留在输入缓冲区。
然后当程序后续调用
let = getchar();
let
会被分配输入缓冲区中的下一个字符 - 甚至无需等待您输入任何其他内容。然后它会在您提供的字符串中搜索此字符 - 但在您提供的示例中找不到它。
在第一个代码片段中调用
gets (input);
不限制用户可以输入的字符数。考虑到函数 gets
不受 C 标准支持并且不安全。
在第二个代码片段中,您将变量 input
声明为
char input[50];
所以用下面的方式调用fgets
就更自然了
fgets (input,sizeof( input ),stdin );
而不是
fgets (input,16,stdin);
标准函数 getchar
读取任何字符,包括白色 spaces。
所以最好使用
scanf( " %c", &let );
否则getchar
可以读取输入缓冲区中留下的任何字符(包括例如换行符)。 scanf
的调用将跳过任何白色 space 字符。
我正在编写一个程序,使用 strchr
查找字符串中特定字符的位置。当使用 fgets
从用户输入字符串时,程序无法正常执行。
然而,当使用 gets
时一切正常。
使用 gets()
的代码:
#include <stdio.h>
#include <string.h>
int main() {
char let;
char input[50];
char *ptr;
printf("what is the string that you would like to check?\n");
gets(input);
printf("what is the character that you would like to find?\n");
let = getchar();
printf("in string \"%s\"...\n", input);
ptr = strchr(input, let);
while (ptr != NULL) {
printf("the letter %c was found at character %d\n", let, ptr - input + 1);
ptr = strchr(ptr + 1, let);
}
return 0;
}
输出:
what is the string that you would like to check? what is the character that you would like to find? in string "why is the world when wondering"... the letter w was found at character 1 the letter w was found at character 12 the letter w was found at character 18 the letter w was found at character 23
无效的代码usgin fgets()
:
#include <stdio.h>
#include <string.h>
int main() {
char let;
char input[50];
char *ptr;
printf("what is the string that you would like to check?\n");
fgets(input, 16, stdin);
printf("what is the character that you would like to find?\n");
let = getchar();
printf("in string \"%s\"...\n", input);
ptr = strchr(input, let);
while (ptr != NULL) {
printf("the character is found at %d \n", ptr - input + 1);
ptr = strchr(ptr + 1, let);
}
return 0;
}
输出:
what is the string that you would like to check? what is the character that you would like to find? in string "abcdefghijklmno"...
改变
fgets(input, 16, stdin)
到
fgets(input, sizeof(input), stdin)
当您将 16
的参数传递给 fgets()
时,您是在指示它读取 不超过 15 个字符 。为什么?
来自 fgets()
联机帮助页:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.
如果您提供多于size -1
个字符,剩余的字符留在输入缓冲区。
然后当程序后续调用
let = getchar();
let
会被分配输入缓冲区中的下一个字符 - 甚至无需等待您输入任何其他内容。然后它会在您提供的字符串中搜索此字符 - 但在您提供的示例中找不到它。
在第一个代码片段中调用
gets (input);
不限制用户可以输入的字符数。考虑到函数 gets
不受 C 标准支持并且不安全。
在第二个代码片段中,您将变量 input
声明为
char input[50];
所以用下面的方式调用fgets
就更自然了
fgets (input,sizeof( input ),stdin );
而不是
fgets (input,16,stdin);
标准函数 getchar
读取任何字符,包括白色 spaces。
所以最好使用
scanf( " %c", &let );
否则getchar
可以读取输入缓冲区中留下的任何字符(包括例如换行符)。 scanf
的调用将跳过任何白色 space 字符。