Skip to content

Latest commit

 

History

History
68 lines (50 loc) · 1.59 KB

File metadata and controls

68 lines (50 loc) · 1.59 KB

C 程序:使用循环从 A 到 Z 显示字符

原文: https://www.programiz.com/c-programming/examples/display-alphabets

在此示例中,您将学习如何打印英文字母的所有字母。

要理解此示例,您应该了解以下 C 编程主题:


打印英文字母的程序

#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 

在此程序中,for循环用于以大写字母显示英文字母。

这是上述程序的一些修改。 该程序根据用户给出的输入以大写或小写形式显示英文字母。


打印小写/大写字母

#include <stdio.h>
int main() {
    char c;
    printf("Enter u to display uppercase alphabets.\n");
    printf("Enter l to display lowercase alphabets. \n");
    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! You entered an invalid character.");
    }

    return 0;
} 

输出

Enter u to display uppercase alphabets. 
Enter l to display lowercase alphabets. 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