Home > Article > Backend Development > How to convert lowercase to uppercase in c language
In C language, ASCII code is used to convert lowercase letters into uppercase letters. The ASCII code value range for lowercase letters is 97-122, and for uppercase letters is 65-90. Therefore, by subtracting 32 from the ASCII code value of the lowercase letters, the corresponding ASCII code value of the uppercase letters can be obtained.
In C language, you can use ASCII code to convert lowercase letters to uppercase letters. In the ASCII code table, the ASCII code value range of lowercase letters a-z is 97-122, while the ASCII code value range of the corresponding uppercase letters A-Z is 65-90. Therefore, we only need to subtract 32 (97-65=32) from the ASCII code value of the lowercase letters to get the corresponding ASCII code value of the uppercase letters.
The following is a simple C language function for converting lowercase letters to uppercase letters:
c
#include <stdio.h> char to_upper(char c) { if (c >= 'a' && c <= 'z') { return c - 'a' + 'A'; } return c; } int main() { char ch = 'b'; printf("小写字母 %c 转换为大写字母是 %c\n", ch, to_upper(ch)); return 0; }
In this example, we define a function called to_upper Function that takes a character as an argument and returns the uppercase version of that character (if it is lowercase). In the main function, we call the to_upper function to convert the lowercase letter b to an uppercase letter and print the result.
Note that in the to_upper function, we first check whether the incoming characters are lowercase letters. If it is, we convert it to uppercase; if not, we just return it. Doing this ensures that our function handles all types of characters correctly.
The above is the detailed content of How to convert lowercase to uppercase in c language. For more information, please follow other related articles on the PHP Chinese website!