做淘宝客优惠券网站还是APP赚钱合肥seo代理商
关注我,持续分享逻辑思维&管理思维; 可提供大厂面试辅导、及定制化求职/在职/管理/架构辅导;
有意找工作的同学,请参考博主的原创:《面试官心得--面试前应该如何准备》,《面试官心得--面试时如何进行自我介绍》 ,《做好面试准备,迎接2024金三银四》。
-------------------------------------正文----------------------------------------
方法一:库函数法
1.小写转换大写:toupper()函数
2.大写转换小写:tolower()函数
#include <iostream>
#include <cctype> // 包含 toupper 和 tolower 函数的头文件int main()
{char lower = 'a';char upper = 'A';// 转换成大写char lower_to_upper = std::toupper(lower);std::cout << "小写字母 '" << lower << "' 转换成大写字母为: '" << lower_to_upper << "'\n";// 转换成小写char upper_to_lower = std::tolower(upper);std::cout << "大写字母 '" << upper << "' 转换成小写字母为: '" << upper_to_lower << "'\n";return 0;
}
方法二:加减32法
1.小写转换大写:字符数据减32
2.大写转换小写:字符数据加32
#include <iostream>
#include <cctype> // 引入用于字符处理的库char toLower(char ch)
{return std::isupper(ch) ? ch + 32 : ch;
}char toUpper(char ch)
{return std::islower(ch) ? ch - 32 : ch;
}int main()
{char ch = 'A'; // 示例字符char ch2= 'a';std::cout << "大写转小写: " << toLower(ch) << std::endl;std::cout << "小写转大写: " << toUpper(ch2) << std::endl;return 0;
}
方法三:位运算法
大小写转换:字符数据按位异或32
int my_move(int ch)
{if (((ch >= 97) && (ch <= 122)) || ((ch >= 65) && (ch <= 90)))return ch ^ 32;elsereturn ch;
}
以上代码实现的是:传入大写字符转换成小写字符,传入小写字符转换成大写字符