如何创建输入的每个单词以开始新行
how to create each word of input to start a new line
嘿,我正在尝试将每个单词打印在一个新行上。我的 EOF 也没有工作,想知道为什么会这样。我已经让它扫描 space 然后打印新行。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
int nextChar;
nextChar = getchar();
while (nextChar != '\n' && nextChar != EOF);
{
if (ch== ' ')
{
printf("\n");
}
else
{
putchar(ch);
}
{
ch = getchar();
}
printf("\n");
{
scanf("%lc",&nextChar);
printf("%c",nextChar);
}
return 0;
}
}
just for example input: Whosebug is great
output:
Whosebug
is
great
您真的应该开始启用编译器警告。他们可以帮助您找到许多错误。当我用 -Wall
和 -Wextra
.
编译时看这里
$ gcc ba.c -Wall -Wextra
ba.c: In function ‘main’:
ba.c:13:5: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
while (nextChar != '\n' && nextChar != EOF);
^~~~~
ba.c:14:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
{
^
删除 while 循环后的 ;
。
但是还有其他问题。从我为您更正缩进时您可以看出,return 0
语句位于 while 循环内。我想这不是你想要的。
嘿,我正在尝试将每个单词打印在一个新行上。我的 EOF 也没有工作,想知道为什么会这样。我已经让它扫描 space 然后打印新行。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
int nextChar;
nextChar = getchar();
while (nextChar != '\n' && nextChar != EOF);
{
if (ch== ' ')
{
printf("\n");
}
else
{
putchar(ch);
}
{
ch = getchar();
}
printf("\n");
{
scanf("%lc",&nextChar);
printf("%c",nextChar);
}
return 0;
}
}
just for example input: Whosebug is great
output:
Whosebug
is
great
您真的应该开始启用编译器警告。他们可以帮助您找到许多错误。当我用 -Wall
和 -Wextra
.
$ gcc ba.c -Wall -Wextra
ba.c: In function ‘main’:
ba.c:13:5: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
while (nextChar != '\n' && nextChar != EOF);
^~~~~
ba.c:14:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
{
^
删除 while 循环后的 ;
。
但是还有其他问题。从我为您更正缩进时您可以看出,return 0
语句位于 while 循环内。我想这不是你想要的。