首页 > 其他 > 详细

String中改变大小写系列函数的用法

时间:2019-04-05 16:12:15      阅读:320      评论:0      收藏:0      [点我收藏+]

1.c语言中的tolower() (变小写) toupper() (变大写)

(1)函数定义的类型为char,因此用string的话要遍历string里面的每个值

(2)使用样例:

{1}#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "ABCDEFG";
    for( int i = 0; i < s.size(); i++ )
    {
        s[i] = tolower(s[i]);
    }
    cout<<s<<endl;
    return 0;
}
结果为abcdefg
{2}
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "abcdefg";
    for( int i = 0; i < s.size(); i++ )
    {
        s[i] = toupper(s[i]);
    }
    cout<<s<<endl;
    return 0;
}
结果为ABCDEFG
2.通过STL的transform算法配合的toupper和tolower来实现该功能就不需要用s[i]的方法
(1)函数的定义类型为char,因为要从string.begin()遍历到 string.end()
(2)使用样例:
{1}#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
    string s = "ABCDEFG";
    string result;
    transform(s.begin(),s.end(),s.begin(),::tolower);
    cout<<s<<endl;
    return 0;
}
结果:abcdefg
{2}#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
    string s = "abcdefg";
    string result;
    transform(s.begin(),s.end(),s.begin(),::toupper);
    cout<<s<<endl;
    return 0;
}
结果:ABCDEFG
3.判断是否为大小写isupper()和islower()
(1)定义类型为char,若与判断的相同则返回1,若不同返回0.
(2)使用样例:
 {1}cout << islower(‘a‘);//输出1
 cout << islower(‘A‘);//输出0
{2}cout << isupper(‘a‘);//输出0
 cout << isupper(‘A‘);//输出1

String中改变大小写系列函数的用法

原文:https://www.cnblogs.com/tankawa/p/10658719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!