How to write code to convert lowercase and uppercase letters in the C language?

Here is an example of C code that converts uppercase letters to lowercase and lowercase letters to uppercase in a string.

#include <stdio.h>

void convertCase(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            // 小写字母转换为大写字母
            str[i] = str[i] - 32;
        } else if (str[i] >= 'A' && str[i] <= 'Z') {
            // 大写字母转换为小写字母
            str[i] = str[i] + 32;
        }
    }
}

int main() {
    char str[100];
    printf("请输入一个字符串:");
    gets(str);

    convertCase(str);

    printf("转换后的字符串为:%s\n", str);

    return 0;
}

The convertCase function in the code is used to switch the upper and lower case letters in a string. The main function takes in a string, calls the convertCase function to switch the letters, and outputs the transformed string.

bannerAds