关于C#:编写密码程序

Writing A Cipher Program

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

我遇到的麻烦是如何进行间距。我创建了一个包含5个对象的数组,但是我不怎么做。这是我到目前为止的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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);
       }
    }
 }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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('\
'
);
           groupCounter = 0;
       }
    }
 }


您无需存储字符即可执行问题中所述的算法。

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

基本上:

1
2
3
4
5
6
7
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 ...