zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C语言:循环输出26个字母

2023-09-27 14:22:46 时间

问题:循环输出 26 个字母。

代码1:

#include <stdio.h>

int main()
{
char c;

for(c = 'A'; c <= 'Z'; ++c)
   printf("%c ", c);

return 0;
}

运行结果:

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

代码2:输出大写或小写字母

#include <stdio.h>

int main()
{
char c;

printf("输入 u 显示大写字母,输入 l 显示小写字母: ");
scanf("%c", &c);

if(c== 'U' || c== 'u')
{
   for(c = 'A'; c <= 'Z'; ++c)
     printf("%c ", c);
}
else if (c == 'L' || c == 'l')
{
    for(c = 'a'; c <= 'z'; ++c)
     printf("%c ", c);
}
else
   printf("Error! 输入非法字符。");
return 0;
}

运行结果:

输入 u 显示大写字母,输入 l 显示小写字母: l
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

代码改进:

1:

#include<stdio.h>

int main(){
int A=65;
int i;
for(i=1;i<=26;i++){
    printf("%c\n",A);
    A++;
}
return 0;
}

2:

#include "stdio.h"

int main() {
int i,n;
ant:printf("输入0或1输出小写或大写:");
scanf("%d",&i); 
if(i!=0 && i!=1) {
    printf("error!输入0或1 !\n");
    goto ant;
} else {
    if(i==0)
    {
    printf("0 小写:");
      for(n=97;n<123;n++)
        printf("%c ",n);
      printf("\n");
    }

    if(i==1) {
        printf("1 大写:");
        for(n=65;n<91;n++)
            printf("%c ",n);
        printf("\n");
    }
}

}