Letter case conversion (multiple input) conventional solutions and solutions using character conversion functions

Preface: In this chapter, we will learn how to convert multiple sets of input data (required by many topics on Niuke.com) to complete letter case conversion, and will show the conventional solution through ASCII code values, as well as simple solutions using character functions.
Niuke.com Sample Questions
insert image description here

How to implement multiple sets of input

Related to multiple groups, everyone must immediately think of the while loop. Yes, multiple sets of input are realized through the while loop, but how to write the judgment conditions of the while loop? we need to knowThe return value of the scanf function

The return value of the scanf function is the number of input data, if you only input one data, then its return value is 1
scanf(“%d%d”,&a,&b) Take this code as an example, its return value is 2

Therefore, if the title requires us to enter only one number at a time,Analyzing conditionsIt can be written as (scanf("%d",&a)) != 1
When you use it, you need to pay attention to adding a bracket outside scanf.overall: while((scanf("%d",&a)) != 1)
In addition, there is a more convenient way of writing:while((scanf("%d",&a)) != EOF)

EOF is the abbreviation of end of file, which is the symbol of the end of the file. It is generally used at the end of the file. In the integrated development environment, it is generally the number -1. As we mentioned above, the return value of scanf is the number of input data , must be greater than -1, so EOF has wider applicability and is more convenient to use.

Letter case conversion (solve the problem by ASCII code value)

To use the ASCII code value to solve the problem, you must first understand the ASCII code value of different letters. Here is an ASCII code table. From the
insert image description here
above table, we can seelowercase letters a ~ zThe ASCII code value range is97 ~ 122,Capital letters A~ZThe ASCII code value range is65 ~ 90. Take A and a as an example, A is 65, a is 90, and their ascii values ​​differ by 32, so A-32 is a, and a+32 is A.

the code

#include<stdio.h>
int main()
{
    
    
	char a = 0; //创建一个字符变量
	while ((scanf("%c", &a)) != EOF) //上文所讲的循环输入
	{
    
    
		if (a >= 65 && a <= 90)    //判断输入字符是否是大写字母
		{
    
    
			a = a + 32;      //大写字母变为小写字母
			printf("%c\n", a);    
		}
		else if(a >= 97 && a <= 122)    //判断输入字符是否是小写字母
		{
    
    
			a = a - 32;    //小写字母转大写字母
			printf("%c\n", a);
		}
	}
	return 0;
}

character function solution

Some students will complain that the ASCII code value is so difficult to remember, and they always forget it, so here is a problem-solving method that does not require ASCII at all.
First of all, we need to understand the following four simple character functions:

Header file #include<ctype.h>
isupper Determines whether it is an uppercase letter, if it is an uppercase letter - returns non-0, if not an uppercase letter - returns 0
islower determines whether it is a lowercase letter, if it is a lowercase letter - returns non-0, if not lowercase - returns 0
toupper converts lowercase to uppercase
tolower converts uppercase to lowercase

Not much to say, just go to the code

the code

#include<stdio.h>
#include<ctype.h>  //不要忘了引用字符函数头文件
int main()
{
    
    
	char a = 0;    //创建字符变量啊、
	while ((scanf("%c", &a)) != EOF)   //循环输入   
	{
    
    
		if(isupper(a))    //判断是否是大写字母
		{
    
    
			a = tolower(a);   //大写字母转小写字母
			printf("%c\n", a);
		}
		else if (islower(a))   //判断是否是小写字母
		{
    
    
			a = toupper(a);  //小写字母转大写字母 
			printf("%c\n", a);
		}
	}
	return 0;
}

BB at the end of the article: It is not easy to make, I hope readers will support me a lot. Your likes and attention are the greatest support for me, thank you.insert image description here

Guess you like

Origin blog.csdn.net/qq_73390155/article/details/129538094