Home > Backend Development > C#.Net Tutorial > How to convert uppercase and lowercase letters in C language?

How to convert uppercase and lowercase letters in C language?

青灯夜游
Release: 2020-08-31 13:43:56
Original
117669 people have browsed it

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).

How to convert uppercase and lowercase letters in C language?

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; then a-32 (or a 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

#include <stdio.h>
int main()
{
    char a,b;
    printf("输入一个字母:\n");
    a=getchar();
	
    if ((a>=&#39;a&#39;) && (a<=&#39;z&#39;)){
			b = a - 32;
			printf("转换后的字母为:%c,%d\n",b,b);
		}
		else if ((a>=&#39;A&#39;) && (a<=&#39;Z&#39;)){
			b = a + 32;
			printf("转换后的字母为:%c,%d\n",b,b);
		}
    return 0;
}
Copy after login

Debug operation result

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

How to convert uppercase and lowercase letters in C language?

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

How to convert uppercase and lowercase letters in C language?

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!

Related labels:
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