[英]counting new lines, new tabs and spaces in C using getchar()
我创建了一个计算新行、新单词、sapces 和标签的表格。 我成功计算了所有这些,但问题出在 output 上:
我试图只用我正在计算的结果打印一行,但 output 是整个计数(增量)。
#include <stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c,nl,nw,nc,state;
int newspace, tabchar;
state = OUT;
int id;
nl = nw = nc = id = 0;
newspace = tabchar = 0;
printf("+===============================================================+\n");
printf("| WORDS, NEW LINE COUNTER |\n");
printf("+======+==========+==========+============+===========+=========+\n");
printf("| ID | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |\n");
printf("+===============================================================+\n");
while ((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
if(c == ' ')
newspace++;
if(c == '\t')
tabchar++;
else if(state == OUT)
{
state = IN;
++nw;
}
id = nl + 1;
printf("| %d | %d | %d | %d | %d | %d |\n", id, nl, nw, nc, newspace, tabchar);
}
}
错误 OUTPUT:
+===============================================================+
| WORDS, NEW LINE COUNTER |
+======+==========+==========+============+===========+=========+
| ID | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
+===============================================================+
I am counting spaces and tab here
| 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 2 | 2 | 1 | 0 |
| 1 | 0 | 2 | 3 | 1 | 0 |
| 1 | 0 | 2 | 4 | 1 | 0 |
| 1 | 0 | 3 | 5 | 2 | 0 |
| 1 | 0 | 3 | 6 | 2 | 0 |
| 1 | 0 | 3 | 7 | 2 | 0 |
| 1 | 0 | 3 | 8 | 2 | 0 |
| 1 | 0 | 3 | 9 | 2 | 0 |
| 1 | 0 | 3 | 10 | 2 | 0 |
| 1 | 0 | 3 | 11 | 2 | 0 |
| 1 | 0 | 3 | 12 | 2 | 0 |
| 1 | 0 | 3 | 13 | 2 | 0 |
| 1 | 0 | 4 | 14 | 3 | 0 |
| 1 | 0 | 4 | 15 | 3 | 0 |
| 1 | 0 | 4 | 16 | 3 | 0 |
| 1 | 0 | 4 | 17 | 3 | 0 |
| 1 | 0 | 4 | 18 | 3 | 0 |
| 1 | 0 | 4 | 19 | 3 | 0 |
| 1 | 0 | 4 | 20 | 3 | 0 |
| 1 | 0 | 5 | 21 | 4 | 0 |
| 1 | 0 | 5 | 22 | 4 | 0 |
| 1 | 0 | 5 | 23 | 4 | 0 |
| 1 | 0 | 5 | 24 | 4 | 0 |
| 1 | 0 | 6 | 25 | 5 | 0 |
| 1 | 0 | 6 | 26 | 5 | 0 |
| 1 | 0 | 6 | 27 | 5 | 0 |
| 1 | 0 | 6 | 28 | 5 | 0 |
| 1 | 0 | 7 | 29 | 6 | 0 |
| 1 | 0 | 7 | 30 | 6 | 0 |
| 1 | 0 | 7 | 31 | 6 | 0 |
| 1 | 0 | 7 | 32 | 6 | 0 |
| 1 | 0 | 7 | 33 | 6 | 0 |
| 2 | 1 | 8 | 34 | 6 | 0 |
所需 OUTPUT:
+===============================================================+
| WORDS, NEW LINE COUNTER |
+======+==========+==========+============+===========+=========+
| ID | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
+===============================================================+
I am counting spaces and tab here
| 2 | 1 | 8 | 34 | 6 | 0 |
我究竟做错了什么? output 似乎几乎是正确的。 在我看来,潜在的问题可能是程序无法正确理解EOF
,特别是以下语句while ((c = getchar()) != EOF)
但我不知道为什么。
感谢您指出解决方案的正确路线。
它实际上运行良好,但您在每次迭代时都在打印。 对于您想要的 output,您应该将printf
语句放在 while 块结束之后。
while ((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
if(c == ' ')
newspace++;
if(c == '\t')
tabchar++;
else if(state == OUT)
{
state = IN;
++nw;
}
id = nl + 1;
printf("| %d | %d | %d | %d | %d | %d |\n", id, nl, nw, nc, newspace, tabchar);
}
应该:
while ((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
if(c == ' ')
newspace++;
if(c == '\t')
tabchar++;
else if(state == OUT)
{
state = IN;
++nw;
}
id = nl + 1;
}
printf("| %d | %d | %d | %d | %d | %d |\n", id, nl, nw, nc, newspace, tabchar);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.