Home > Backend Development > C++ > How to convert lowercase to uppercase in c language

How to convert lowercase to uppercase in c language

小老鼠
Release: 2024-03-25 14:15:37
Original
1440 people have browsed it

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.

How to convert lowercase to uppercase in c language

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 >= &#39;a&#39; && c <= &#39;z&#39;) {  
        return c - &#39;a&#39; + &#39;A&#39;;  
    }  
    return c;  
}  
  
int main() {  
    char ch = &#39;b&#39;;  
    printf("小写字母 %c 转换为大写字母是 %c\n", ch, to_upper(ch));  
    return 0;  
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template