[c/cpp]:字母大小写转换
一、代码
1 #include <iostream>
2 #include <string>
3 #include <vector>
4
5
6 char ip1[] = "01234567890AbcDEf";
7 char ip2[] = "01234567890AbcDEf";
8
9
10 // convert lower character to upper character(A-Z)
11 void l2u(char *p)
12 {
13
14 // berfore:
15 std::cout << "\t[in]#\t" << p << std::endl;
16
17 char *x=p;
18 for (; *x!='\0'; x++) {
19 if (*x>=97 && *x<=122) {
20 *x -= 32;
21 }
22 }
23
24 // after
25 std::cout << "\t[out_upper]#\t" << p << std::endl;
26 }
27
28
29 // convert upper character to lower character(a-z)
30 void u2l(char *p)
31 {
32
33 // berfore:
34 std::cout << "\t[in]#\t" << p << std::endl;
35
36 char *x=p;
37 for (; *x!='\0'; x++) {
38 if (*x>=65 && *x<=90) {
39 *x += 32;
40 }
41 }
42
43 // after
44 std::cout << "\t[out_lower]#\t" << p << std::endl;
45 }
46
47
48 // test part
49 int main()
50 {
51 // convert input characters to upper characters
52 l2u(ip1);
53
54 // convert input characters to lower characters
55 u2l(ip2);
56
57 return 0 ;
58 }
二、运行结果
[in]# 01234567890AbcDEf
[out_upper]# 01234567890ABCDEF
[in]# 01234567890AbcDEf
[out_lower]# 01234567890abcdef
三、参考资料及工具
1、 cpp在线编译工具 - https://coliru.stacked-crooked.com/
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/18522877