C++ 如何将string转换成全小写
更新时间:2022年11月09日 09:14:13 作者:zing2000
这篇文章主要介绍了C++ 如何将string转换成全小写问题,具有很好的参考价值,希望对大家有所帮助。
如何将string转换成全小写
#include <iostream>
#include <string>
#include <algorithm>
using std::cout;
using std::endl;
void main()
{
std::string str;
str.assign("Hello World!");
std::transform(str.begin(),str.end(),str.begin(),tolower); // or 'toupper'.
cout<<str.c_str()<<endl;
}string字符串大小写转换的两种方式
这里提供两种对c++中string字符串进行大小写转换的方式(windows系统vs)
第一种方式:下标
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin >> str; //注意这里对于中间有空格的单词只会将第一个空格前的单词大写
//getline(cin, str); 可以将一整行的单词大写,两种方式看个人需求取其一即可
for (int i = 0; i < str.size(); i++)
str[i] = toupper(str[i]);
cout << str << endl;
return 0;
}第二种方式:迭代器
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
//cin >> str; //注意这里对于中间有空格的单词只会将第一个空格前的单词大写
getline(cin, str); //可以将一整行的单词大写,两种方式看个人需求取其一即可
for (auto it1 = str.begin(); it1 != str.end(); it1++)
{
*it1 = toupper(*it1);
}
cout << str << endl;
return 0;
}
//另外如果要将单词化为小写,将toupper换成tolower即可以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章
C++详解使用floor&ceil&round实现保留小数点后两位
这篇文章主要介绍了C++使用floor&ceil&round实现保留小数点后两位的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-07-07


最新评论