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

How to convert lowercase to uppercase in c language

藏色散人
藏色散人 Original
2020-03-03 11:03:24 21221browse

How to convert lowercase to uppercase in c language

How to convert lowercase to uppercase in c language?

In C language, letters are case-sensitive. Using the conversion relationship between uppercase letters and lowercase letters in ASCII code (the difference is 32), lowercase letters can be converted into uppercase letters. Write a program to implement it. Enter a lowercase letter from the keyboard and press the Enter key. The program will convert the lowercase letter to an uppercase letter and output its ASCII value.

Recommended: "C Language Tutorial"

Algorithmic Thought

Due to the difference between uppercase letters and lowercase letters is 32, so the way to convert lowercase letters into uppercase letters is to subtract 32 from the ASCII code value of the lowercase letters to get the corresponding uppercase letters.

Use the getchar function to input a lowercase letter from the keyboard and assign it to a character variable a; then assign the value of a-32 to the character variable b; finally output, with the letters output first, Then output the letters as integers. The specific steps are as follows:

① Define two character variables a and b;

② a=get char();

③ b=a-32;

④ Print output.

Program code

#include  int main() { char a,b; printf("输入一个小写字母:\n"); a=getchar(); b=a-32; printf("转换后的字母为:%c,%d\n",b,b); return 0; }

Debug operation result

When the lowercase letter c is entered, the converted uppercase letter and the corresponding ASCII value are as follows:

Enter a lowercase letter:

c

The converted letter is: C, 67

When you enter the lowercase letter m, the converted uppercase letter and the corresponding ASCII value are as follows:

Enter a lowercase letter:

m

The converted letter is: M, 77

Summary

① Example content requirements ASCII code is understood. Know that the difference between lowercase letters and uppercase letters is 32.

② The function of getchar function is to input a character from the keyboard. Its general form is "getchar()". Usually the input characters are assigned to a character variable to form an assignment statement, such as: char c; or c=getchar();

③ When using the getchar function, you should also pay attention to several issues:

getchar The function can only accept single characters, and input numbers are also treated as characters. When entering more than one character, only the first character is received.

The file "stdio.h" must be included before using the getchar function.

For more programming related content, please pay attention to theProgramming Introductioncolumn on the php Chinese website!

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!

Statement:
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