= 'A' && c..._c语言大小写转换">
赞
踩
方法一:
- #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;
- }
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。