getchar函数使用问题
#include<stdio.h>main()
{
char str1;
char str2;
char ch = NULL;
char ch1 = NULL;
int m = 0;
printf("Enter the first string:");
ch = getchar();
while((ch=getchar())!='\n')
{
str1 = ch;
m++;
}
str1 = '\0';
m = 0;
printf("Enter the second string:");
ch1 = getchar();
while((ch1=getchar())!='\n')
{
str2 = ch1;
m++;
}
str2 = '\0';
m = 0;
printf("The first string is %s.\n",str1);
printf("The second string is %s.\n",str2);
}
程序可以运行,但这里输入的两个字符串中第一个输入的字符都被忽略了,求解决.... #include<stdio.h>
main()
{
char str1;
char str2;
char ch = NULL;
char ch1 = NULL;
int m = 0;
printf("Enter the first string:");
//这是你输入的第一个字符,在循环外面,自然被忽略了。
ch = getchar();
while((ch=getchar())!='\n')
{
str1 = ch;
m++;
}
str1 = '\0';
m = 0;
printf("Enter the second string:");
//这是你输入的第一个字符,在循环外面,自然被忽略了。
ch1 = getchar();
while((ch1=getchar())!='\n')
{
str2 = ch1;
m++;
}
str2 = '\0';
m = 0;
printf("The first string is %s.\n",str1);
printf("The second string is %s.\n",str2);
} xieglt 发表于 2020-11-17 08:38
非常感谢{:10_266:}
页:
[1]