string相关函数头⽂件#include <cctype>
输出包含的标点符号
#include <iostream>
#include <string>
字符串截取函数有哪些#include <cctype>
int main()
{
std::string s1 = "lolita is a beautiful girl ? yes !";
//输出其中的标点符号
for(auto c : s1)
if(ispunct(c))
std::cout<<c<<std::endl;
return0;
}
将所有⼩写转换为⼤写字母
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string s1 = "lolita is a beautiful girl ? yes !"; for(auto &c : s1)
if(islower(c))
c = toupper(c);
std::cout<<s1<<std::endl;
return0;
}
将⾸个单词⼤写
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string s1 = "lolita is a beautiful girl ? yes !"; for(decltype(s1.size()) index = 0 ;
index<s1.size()&&!isspace(s1[index]);
index++)
{
s1[index] = toupper(s1[index]);
}
std::cout<<s1<<std::endl;
return0;
}
判断⾸字母⼤⼩写
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string s1 = "lolita is a beautiful girl ? yes !"; if(s1.size()>0) //很重要
{
if(isupper(s1[0]))
{
std::cout<<"⾸字母⼤写"<<std::endl;
}
else
{
std::cout<<"⾸字母⼩写"<<std::endl;
}
}
else
{
std::cout<<"空字符串"<<std::endl;
}
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。