编写密码程序

时间:2013-02-05 17:27:18

标签: c encryption

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

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

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    char c=0, block[4]; 

    while (c != EOF)
    {
       c=getchar();

       if (isupper(c))
       {
           putchar(c);
       }
       if (islower(c))
       {
          putchar(c-32);
       }
    }
 }

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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;
       }
    }
 }