在此利用STL的transform配合toupper/tolower,完成std::string轉換大(小)寫的功能,也看到Generics的威力,一個transform function,可以適用於任何型別,且只要自己提供Algorithm,就可完成任何Transform的動作。
这是字符串匹配中经常需要做的事情,然而C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写(toupper)和小写(tolower)的功能而已。 但我们可以利用STL的transform配合toupper/tolower,完成std::string转换大(小)写的功能,也看到 模版编程 的威力了,一个transform函数,可以...
string s1; // 初始化一个空字符串 string s2 = s1; // 初始化s2,并用s1初始化 string s3(s2); // 作用同上 string s4 = "hello world"; // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中 string s5("hello world"); // 作用同上 string s6(6,'a'); // 初始化s6为:...
2.tolower()函数: 程序代码: #include<iostream> using namespace std; int main(){ char a[] = "woAiX"; for(int i=0;i<5;i++){ a[i]=tolower(a[i]); } printf("%s",a); return 0; } 运行结果:
1、tolower()函数。inttolower(intc);2、toupper()函数 inttoupper(intc);例:include<stdio.h> include<ctype.h> intmain(void){ charch='A';ch=tolower(ch);printf("ch=%c\n",ch);ch='b';ch=toupper(ch);printf("ch=%c\n",ch);return0;} ...
2、string类也可以自己手写两个转化为大写和小写transform()方法,如下所示: #include <iostream> #include <algorithm> #include <cstring> using namespace std; void mytolower(string& s){ int len=s.size(); for(int i=0;i<len;i++){
{string[i] = tolower(string[i]); } printf("%s\n",string);system("pause"); } c++程序例:include <iostream> include <string> include <cctype> using namespace std;int main(){ string str= "THIS IS A STRING";for (int i=0; i <str.size(); i++) { str[i] = to...
<limits.h> C库 - <locale.h> C库 - <math.h> C库 - <setjmp.h> C库 - <signal.h> C库 - <stdalign.h> C库 - <stdarg.h> C库 - <stdbool.h> C库 - <stddef.h> C库 - <stdio.h> C库 - <stdlib.h> C库 - <string.h> C库 - <tgmath.h> C库 - C库 - <wctype.h>...
usingnamespacestd; intmain(intargc,charconst*argv[]) { // We want to convert"ABC" to"aBC" string S="ABC"; S[0]+=0x20; // Returns"aBC" cout<<S<<endl; return0; } 相关讨论 有关工作原理,请参见Rahul Sharma的回答:stackoverflow.com/a/38707004/11087678 ...
在C语言中,可以使用标准库函数tolower()来将大写字母转换成小写字母。tolower()函数的原型定义在<ctype.h>头文件中,其函数签名如下:该函数接受一个字符参数c,并将其转换成小写字母后返回。如果c不是大写字母,则该函数返回c本身。下面是一个示例程序,它从标准输入中读取一行文本,将其中的大写字母...