C语言实现大小写转换的三种方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_19734597/article/details/102758797

实现大小写转换的三种方法

方法一:

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char str[] = "AbCdEf";
 char c;
 int i = 0;
 while (str[i] != '\0')
 {
  c = str[i];
  if (c >= 'A' && c <= 'Z')
  {
   c = c + 32;
  }
  else if (c >= 'a' && c <= 'z')
  {
   c = c - 32;
  }
      printf("%c",c);
  i++;
 }
 printf("\n");
 system("pause");
 return 0;
}

运行结果如下:

在这里插入图片描述

方法二: 

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char str[] = "AbCdEf";
 char c;
 int i = 0;
 while (str[i] != '\0')
 {
  c = str[i];
  if (c >= 'A' && c<='Z' || c>='a' && c <= 'z')
  {
   c ^= 32;
  }
  printf("%c", c);
  i++;
 }
 printf("\n");
 system("pause");
 return 0;
}

运行结果如下:

在这里插入图片描述

方法三:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main()
{
 char str[] = "AbCdEf";
 char c;
 int i = 0;
 while (str[i] != '\0')
 {
  c = str[i];
  if (isupper(c))
  {
   c = tolower(c);
  }
  else if (islower(c))
  {
   c = toupper(c);
  }
  printf("%c", c);
  i++;
 }
 printf("\n");
 system("pause");
 return 0;
}

运行结果如下:

在这里插入图片描述

小写转大写封装 :

​uint8_t low2UpperChar(uint8_t *in, int inlen, uint8_t *out) {
	char c;
	for (int i = 0; i < inlen; i++)
	{
		c = in[i];
		if ('a' <= c && c <= 'z')
		{
			out[i] = c - 32;
		}
		else
		{
			out[i] = c;
		}
	}
	return 0;
}

​

猜你喜欢

转载自blog.csdn.net/qq_19734597/article/details/102758797