【问题标题】:Checking abc alphabet on C language在 C 语言上检查 abc 字母
【发布时间】:2017-05-13 14:08:51
【问题描述】:

我刚开始学习 C 语言的循环。

编写一个程序,检查输入的abc字母是否正确,输入的是abc字母的小写字母,假设输入是有序的,如果遗漏了一些后者你应该在缺失的后者后面加上大写。输入有一个 $ 符号。

例子:

对于输入:abcdijklmnstuvyz$ 应该打印 abcdEFGHijklmnOPQRstuvWXyz

对于输入:abefghijkopqrvwxyz$ 应该打印 abCDefghijkLMNopqrSTUvwxyz

我的想法是在 'a,b,c' 字母表上使用两个数组,在更正后使用另一个数组,这是我的代码:

#include <stdio.h>

int main()
{
    char student[26] = {0};
    char real[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'c', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char corection[26] = {0};
    int istudent = 0, ireal = 0, icorection = 0;

    for(istudent = 0; istudent<26; istudent++)//changed to < instad of <=
    scanf("%c", &student[istudent]);

    for(istudent = ireal = icorection = 0; (istudent < 26) && (ireal < 26); icorection++)
    {
        if (student[istudent] == real[ireal])
           {
           istudent++;
           ireal++;
           corection[icorection] = student[istudent];
           }
        if (student[istudent] != real[ireal])
           {
           istudent++;
           ireal++;
           corection[icorection] = (real[ireal] - 32);
           }

    }
   // print results
    int k;
    printf("printed array: \n");
    for(k=0;k<26;k++)
        printf("%c", corection[k]);
    return 0;
}

我正在尝试打印结果以检查我是否编写了正确的代码,但它没有显示正确的输出

【问题讨论】:

  • 抱歉,有什么问题?
  • 现在是开始学习如何使用调试器的好时机。

标签: c arrays loops character


【解决方案1】:
for(istudent = 0; istudent<26; istudent++)//changed to < instad of <=
    scanf("%c", &student[istudent]);

这是您的代码/逻辑中的问题。您希望用户始终输入 26 个字符且仅输入 26 个字符。

你需要的是,当你看到 $ 时停下来

这样您就可以将循环更改为

for(istudent = 0; istudent<26; istudent++){
    scanf("%c", &student[istudent]);
    if(student[istudent] == '$')
        break;
}

或者您可以使用

读取字符串字符串
scanf("%s", student);

其次,您总是检查 for 循环内的两个条件。如果第一个条件为真,您希望跳过第二个条件。

所以你可以使用 if - else if 。

将代码更改为 -

else if (student[istudent] != real[ireal])

最后在条件里面,因为你是先递增,所以在访问数组时需要使用istudent -1

istudent++;
ireal++;
corection[icorection] = student[istudent-1];

另外你不应该在第二种情况下增加学生,否则你会跳过字符。

这是一个Demo,已修复所有错误。

编辑:

根据 PeterPaulKiefer 的建议,您可以进行多项改进 首先 - ire​​al 和 icorrection 总是一起改变并且总是具有相同的值。您可以消除其中之一。

其次,需要检查第二个条件,如果第一个为假,则第二个必然为真。所以你可以写else

最后,您可以在索引到数组后更改 istudent 和 ireal 的增量,以获得更好的可读性。

corection[icorection] = student[istudent];
istudent++;

【讨论】:

  • 这是一个非常好的答案,但我会使用icorrection 而不是ireal,因为它们在使用时总是相等的。然后每次使用student[istudent++],并在上面两次删除istudent++。第二个 if 也只是一个 else。 ;-)
  • @PeterPaulKiefer,我同意,很多事情都可以改变。但我的意图是让代码尽可能与 OP 代码相似,这样他就能看到对比。我可以添加您建议的改进以使代码更好。
  • 我的评论不是对您的回答的批评。我明白,你提出了这一点,并给了 OP 一个正确的工作答案。但是当我看到你更快时,我正要自己给出答案;-)。而且因为你的包括我没有看到的东西,我不想在你的旁边给出我的答案。所以我用评论来表明我的不同之处。干杯...
【解决方案2】:

首先,你的真实字母是错误的,应该是:

char real[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

你用 c 代替了 s。


为了解析输入,你要一直读到终止字符输入,也就是美元符号!结果,由于您不知道要输入多少个字符(假设它们将小于 27(包括美元符号),请使用 ,如下所示:

char input[26] = {0};
int i = 0;
char c;
while(1)
{
    scanf("%c", &c);
    if(c != '$')
        input[i++] = c;
    else
        break;
}

在第一次读取字符的地方,检查它是否是终止符号,如果不是,则将其插入到存储输入的 input 缓冲区中。

然后你想输出想要的字符。我看到你想构造一个包含它们的数组,所以我遵循了这种方式,但请注意你可以简单地打印当时的字符。

因此,使用,因为您知道迭代次数,然后循环遍历real 数组。当您与input 匹配时,只需将该字符插入output 数组。

如果不这样做,将对应的字符从real 转换为大写并插入到output 数组中。

当然,您需要两个计数器来执行此操作,一个用于realoutput 数组,i 在我的代码中,一个用于input 数组,j 在我的代码中。

int j = 0;
for(i = 0; i < 26; ++i)
{
    //printf("%d %d %c %c\n", i, j, real[i], input[j]);
    if(real[i] == input[j])
        output[i] = input[j++];
    else
        output[i] = toupper(real[i]);
}

你就完成了!


完整代码:

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

int main(void)
{
    char input[26] = {0};
    char real[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char output[26] = {0};

    int i = 0;
    char c;
    while(1)
    {
        scanf("%c", &c);
        if(c != '$')
            input[i++] = c;
        else
            break;
    }

    int j = 0;
    for(i = 0; i < 26; ++i)
    {
        printf("%d %d %c %c\n", i, j, real[i], input[j]);
        if(real[i] == input[j])
            output[i] = input[j++];
        else
            output[i] = toupper(real[i]);
    }

    for(i = 0; i < 26; ++i)
        printf("%c", output[i]);
    printf("\n");

    return 0;
}

输出:

abcdEFGHijklmnOPQRstuvWXyz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2016-01-13
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多