Exercise>>Code implementation to change lowercase letters in a string to uppercase

analyze:

Since the characters are stored with the corresponding ASCII code value when they are stored in the computer; and the ASCII code values ​​of the letters are continuous;

So we can control the conversion of uppercase and lowercase letters through addition and subtraction; as can be seen from the ASCII code in the figure below, the difference between uppercase and lowercase letters is only 32 ; so only lowercase letters need to be subtracted by 32 to convert them to uppercase letters



Code:

#include <stdio.h>
#include <string.h>

void fun(char tt[])
{
	int i = 0;
	int len = strlen(tt);
	for (i = 1; i < len; i+=2)
	{
		if (tt[i] > 97 && tt[i] < 122)
		{
			tt[i] = tt[i] - 32;
		}
	}
}
int main()
{
	char tt[81] = { 0 };
	printf("请输入一个小于80的字符串\n");
	gets(tt);
	fun(tt);
	printf("改变后字符串为:%s", tt);
	return 0;
}



Guess you like

Origin blog.csdn.net/2301_77509762/article/details/130812828