Home>Article>Backend Development> How to convert uppercase and lowercase letters in C language?
In C language, the method to convert lowercase letters to uppercase letters is to subtract 32 from the ASCII code value of the lowercase letters (for example: A=a-32); the method to convert uppercase letters to lowercase letters is Just add 32 to the ASCII code value of the uppercase letters (for example: a=A 32).
In C language, we distinguish between uppercase and lowercase letters and use the conversion relationship between uppercase letters and lowercase letters in ASCII code (the difference is 32). Convert lowercase letters to uppercase letters; or convert uppercase letters to lowercase letters. Tutorial recommendation: "c Language Tutorial Video"
Write a program to implement it. Enter a lowercase letter (or uppercase letter) from the keyboard, press the Enter key, and the program will Convert letters to uppercase letters (or uppercase letters to lowercase letters) and output their ASCII values.
Algorithm Thought
Since the difference between uppercase letters and lowercase letters is 32, the way to convert lowercase letters to uppercase letters is to convert the ASCII code of the lowercase letters Subtract 32 from the value to get the corresponding uppercase letter; the method to convert an uppercase letter to a lowercase letter is to add 32 to the ASCII code value of the uppercase letter to get the corresponding lowercase letter
Use the getchar function to enter a lowercase letter (or uppercase letter) from the keyboard and assign it to a character variable a; thena-32
(ora 32
) The value is assigned to the character variable b; it is output at the end. When outputting, the letters are output first, and then the letters are output in the form of integers. The specific steps are as follows:
① Define two character variables a and b;
② a=getchar();
③ b=a-32; (b= a 32)
④ Print output.
Program code
#includeint main() { char a,b; printf("输入一个字母:\n"); a=getchar(); if ((a>='a') && (a<='z')){ b = a - 32; printf("转换后的字母为:%c,%d\n",b,b); } else if ((a>='A') && (a<='Z')){ 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 its corresponding The ASCII value is as follows:
When the lowercase letter M is entered, the converted uppercase letter and the corresponding ASCII value are as follows:
Summary
① The content of the example requires some understanding of ASCII codes. 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();
③ There are several issues that should be paid attention to when using the getchar function:
The getchar function can only receive a single character, and input numbers are also processed 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 knowledge, please visit:Programming Teaching! !
The above is the detailed content of How to convert uppercase and lowercase letters in C language?. For more information, please follow other related articles on the PHP Chinese website!