C程序计算字和行的标准输入 [英] C Program that counts Words and Lines in Standard input
问题描述
我是新来的C语言编程,我目前正试图教我如何建立一个C程序,可以计算字数和线条输入流中,并打印两总计到标准输出。
I am new to C programming and I am currently trying to teach myself how to create a C program that can count words and lines in the input stream and print the two totals to the standard output.
什么我其实想做的是有计划计算行数和计数取决于一个词的定义,我觉得我过的单词数。
What I am actually trying to do is to have the program count the number of lines and count the number of words depending on the definition of a word in which I feel that I am off.
我想的话,排除空格,制表符,换行符,连字符或冒号。虽然有作为小数的程序输出结果(文字和线条)。
I want the words to exclude blanks, tabs, newlines, hyphens, or colons. While having the program output the results (words and lines) as decimals.
#include<stdio.h>
int main()
{
int iochar;
int words;
int lines;
printf("Enter something here:\n\n");
while ((iochar = getchar ()) !=EOF)
{
if((iochar == ' ') || (iochar == '\t') || (iochar == '\n'))
putchar(iochar);
}
return 0;
}
我是完全关闭这个程序?
Am I totally off on this program?
推荐答案
如果你的问题是如何解决的编译错误,这很简单。在最后增加一个右括号。
If your question is how to fix the compile error, that's simple. Add one more closing brace at the end.
不过你的程序仍然会做只有一个一次通过循环,只打印一个字符当且仅当用户键入一个空格,制表符或换行符。无论什么类型的用户,该程序将终止运行。我怀疑这是你想要的。
But your program will still do only one pass through the loop and will print only one character if and only if the user types a space, tab or newline. No matter what the user types, the program will then terminate. I doubt that's what you wanted.
我怀疑这是你想要的结果:
I suspect this is what you intended:
while ((iochar = getchar ()) !=EOF)
{
if((iochar == ' ') || (iochar == '\t') || (iochar == '\n'))
{
putchar(iochar);
}
}
return 0;
这篇关于C程序计算字和行的标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!