我刚开始学习C语言的循环。
编写一个程序,检查abc字母表的输入是否正确,输入是abc字母表中的小字母,假设输入是有序的,如果输入有问题,你应该添加缺少后者的资本。输入有一个$符号。
exampls:
输入: 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;
}
我打印结果以检查我是否写了正确的代码,但它没有显示正确的输出
答案 0 :(得分:3)
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的建议,您可以做几项改进 首先 - ireal和icorrection总是一起改变,并且总是具有相同的价值。你可以消除其中一个。
其次,需要检查第二个条件,如果first为false,则second必然为true。所以你可以写else
。
最后,您可以在索引到数组后更改istudent和ireal的增量,以提高可读性。
corection[icorection] = student[istudent];
istudent++;
答案 1 :(得分:3)
首先,你的真实字母是错误的,应该是:
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(包括美元符号),请使用while-loop,如下所示:
char input[26] = {0};
int i = 0;
char c;
while(1)
{
scanf("%c", &c);
if(c != '$')
input[i++] = c;
else
break;
}
首先读取一个字符,检查它是否是终止符号,如果不是,则将其插入存储输入的input
缓冲区。
然后您想输出所需的字符。我看到你想要构造一个包含它们的数组,所以我遵循这个方式,但请注意你可以简单地打印这个角色。
因此,使用for-loop,因为您知道迭代次数,并循环遍历real
数组。当您与input
匹配时,只需将该字符插入output
数组即可。
如果不这样做,请将相应的字符从real
转换为大写字母并将其插入output
数组。
当然,您需要两个计数器来执行此操作,一个用于real
和output
数组,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