博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何將std::string轉成大寫(小寫)? (C/C++) (STL) (C)
阅读量:4198 次
发布时间:2019-05-26

本文共 1182 字,大约阅读时间需要 3 分钟。

Abstract

C++的Standard Library並沒有提供將std::string轉成大寫和小寫的功能,只有在<cctype>提供將char轉成大寫(toupper)和小寫(tolower)的功能而已,在此利用STL的transform配合toupper/tolower,完成std::string轉換大(小)寫的功能,也看到Generics的威力,一個transform function,可以適用於任何型別,且只要自己提供Algorithm,就可完成任何Transform的動作。
C++
1 /* 
 2 (C) OOMusou 2008 http://oomusou.cnblogs.com
 3 
 4 Filename    : StringToUpper.cpp
 5 Compiler    : Visual C++ 8.0
 6 Description : Demo how to upper string in C++
 7 Release     : 04/03/2008 1.0
 8 */
 9 #include <iostream>
10 #include <string>
11 #include <cctype>
12 #include <algorithm>
13 
14 using namespace std;
15 
16 int main() {
17   string s = "Clare";
18   // toUpper
19   transform(s.begin(), s.end(), s.begin(), toupper);
20   
21   // toLower
22   //transform(s.begin(),s.end(),s.begin(),tolower);
23 
24   cout << s << endl;
25 }

 

C語言

1 /* 
 2 (C) OOMusou 2008 http://oomusou.cnblogs.com
 3 
 4 Filename    : toupper.c
 5 Compiler    : Visual C++ 8.0
 6 Description : Demo how to upper string in C
 7 Release     : 04/03/2008 1.0
 8 */
 9 #include <stdio.h>
10 #include <ctype.h>
11 
12 int main() {
13   char s[] = "Clare";
14   int i = -1;
15   
16   while(s[i++]) 
17     s[i] = toupper(s[i]);
18     // s[i] = tolower(s[i]);
19   
20   puts(s);  
21 }

 

Reference

Danny Kalev,  , DevX

 

转载自:

 

你可能感兴趣的文章
Ajax中的get和post两种请求方式的用法
查看>>
7种流行PHP集成开发工具(IDE)的比较和环境培植
查看>>
给图片链接加边框,时,ff和chrome的bug问题
查看>>
关于HTML语言中的width和height属性的百分比表示
查看>>
Android开发学习 之 五、基本界面控件-4时间控件
查看>>
详细解读Jquery的$.get(),$.post(),$.ajax(),$.getJSON()用法
查看>>
同步与异步的区别
查看>>
Python定时任务框架apscheduler,定时执行多个固定任务
查看>>
python定义一个装饰器自动测量函数的运行时间
查看>>
语义化版本管理(Semantic Versioning)
查看>>
IT行业--简历模板及就业秘籍
查看>>
JAVA处理Clob大对象
查看>>
计院生活--第二章 深入虎穴(上)
查看>>
计院生活--第二章 深入虎穴(下)
查看>>
JNI简介及实例
查看>>
Quartz入门到精通
查看>>
OGNL表达式语言介绍
查看>>
DOM4J使用教程
查看>>
JAVA实现文件树
查看>>
Drools 规则引擎
查看>>