gpt4 book ai didi

c - 我需要编写一个利用 ASCII 列出字母表的程序

转载 作者:行者123 更新时间:2023-11-30 21:09:16 24 4
gpt4 key购买 nike

我正在制作一个程序,提示用户输入并从键盘读取两个字符,一次一个。该程序将打印所有从输入的第一个字母到最后一个字母(含)的 ASCII 字母。感谢 Eric J,我现在可以循环工作了。尽管它在最终显示我想要的字母字符串之前输出了 ASCII 字母、数字和符号的整个列表。

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

int main(int argc, char *argv[]) {
char charone, chartwo, curr;
curr = charone;

printf("Enter the first character:");
scanf(" %c", &charone);
printf("Enter the last character: ");
scanf(" %c", &chartwo);

while(curr <= chartwo) {
printf("Your character is %c\n", curr++);
}

system("PAUSE");
return 0;
}

最佳答案

while 关键字全部小写。 While 不应编译。

如果目标是打印从第一个字符到最后一个字符的所有字符(如果第一个字符在字母表中的位置晚于最后一个字符,则不显示任何字符,请考虑使用此 while 循环

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

int main(int argc, char *argv[]) {

char charone, chartwo;

printf("Enter the first character:");
scanf(" %c",&charone);
printf("Enter the last character: ");
scanf(" %c",&chartwo);

char curr = charone;
while(curr <= chartwo)
{
printf("Your character is %c\n",curr);
curr++; // You can also do ++ in the previous line. Used 2 lines for clarity.
}

return 0;

}

您还可以使用for循环

for (char curr = charone; curr <= chartwo; curr++)
{
printf("Your character is %c\n",curr);
}

关于c - 我需要编写一个利用 ASCII 列出字母表的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35026486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com