我正在尝试
getchar()
功能。有人可以解释一下为什么每次我用空格完成输入行然后按 Enter 键时,输入窗口都会跳过一行,然后我必须再次按 Enter 键才能继续执行代码。
谢谢你
#include <stdio.h>
#include <ctype.h>
#define MAX_SIZE 6 //+1 space for the 0
int main()
{
char array[MAX_SIZE] = {0};
char ch = 0;
while(1)
{
int i = 0;
while((ch = getchar()) != EOF
&& ch != '\n'
//&& ch != ' '
&& i < MAX_SIZE - 1)
{
if(islower(ch))
ch = toupper(ch);
else if(isupper(ch))
ch = tolower(ch);
while(ch == ' ')
ch = getchar();
array[i++] = ch;
}
array[i] = '\0';
while (ch != '\n' && ch != EOF)
ch = getchar();
printf("%s\n", array);
}
return 0;
}
ch
的类型错误。必须是int
而不是char
int main()
{
char array[MAX_SIZE] = {0};
int ch = 0;
while(1)
{
size_t i = 0;
while((ch = getchar()) != EOF && ch != '\n' && i < MAX_SIZE - 1)
{
if(islower(ch))
ch = toupper(ch);
else if(isupper(ch))
ch = tolower(ch);
if(ch == ' ') continue;
array[i++] = ch;
}
array[i] = '\0';
printf("\"%s\"\n", array);
}
return 0;
}