编写密码程序

编写一个程序(filter),从标准输入读取ASCII流并将字符发送到标准输出。 该程序丢弃除字母以外的所有字符。 任何小写字母都以大写字母输出。 以空格字符分隔的五个组中的输出字符。 每10组后输出一个换行符。 (一行中的最后一个组仅跟随换行符;一行上的最后一个组后面没有空格。)最后一组可能少于五个字符,最后一行可能少于10个组。 假设输入文件是任意长度的文本文件。 为此使用getchar()和putchar()。 您永远不需要一次在内存中包含多个输入数据字符

我遇到的麻烦是如何做间距。 我创建了一个包含5个对象的数组,但我不知道如何处理它。 这是我到目前为止:

#include  #include  #include  int main() { char c=0, block[4]; while (c != EOF) { c=getchar(); if (isupper(c)) { putchar(c); } if (islower(c)) { putchar(c-32); } } } 

 int main() { char c=0; int charCounter = 0; int groupCounter = 0; while (c != EOF) { c=getchar(); if (isupper(c)) { putchar(c); charCounter++; } if (islower(c)) { putchar(c-32); charCounter++; } // Output spaces and newlines as specified. // Untested, I'm sure it will need some fine-tuning. if (charCounter == 5) { putchar(' '); charCounter = 0; groupCounter++; } if (groupCounter == 10) { putchar('\n'); groupCounter = 0; } } } 

您不需要存储字符以执行问题中描述的算法。

你应该一次阅读一个字符,并跟踪我不会透露的2个计数器。 每个计数器将允许您知道在何处放置格式化输出所需的特殊字符。

基本上:

 read a character if the character is valid for output then convert it to uppercase if needed output the character update the counters output space and or newlines according to the counters end if 

希望这可以帮助。

另外:我不知道你试图用block变量做什么,但它被声明为一个包含4个元素的数组,文本中没有任何地方使用数字4 …