练习>>代码实现将一个字符串中的小写字母变为大写

分析:

由于字符在计算机中存储时是用对应的ASCII码值来存储;并且字母的ASCII码值是连续的;

所以我们可以通过加减来控制大小字母的转换;通过下图ASCII码可以看出,大小写只相差32;所以只需要小写字母减去32就可以转换为大写字母



代码实现:

#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;
}



猜你喜欢

转载自blog.csdn.net/2301_77509762/article/details/130812828