在此利用STL的transform配合toupper/tolower,完成std::string轉換大(小)寫的功能,也看到Generics的威力,一個transform function,可以適用於任何型別,且只要自己提供Algorithm,就可完成任何Transform的動作。
string s3(s2); // 作用同上 string s4 = "hello world"; // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中 string s5("hello world"); // 作用同上 string s6(6,'a'); // 初始化s6为:aaaaaa string s7(s6, 3); // s7 是从 s6 的下标 3 开始的字符拷贝 string s8(s...
这是字符串匹配中经常需要做的事情,然而C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写(toupper)和小写(tolower)的功能而已。 但我们可以利用STL的transform配合toupper/tolower,完成std::string转换大(小)写的功能,也看到 模版编程 的威力了,一个transform函数,可以...
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;} ...
#include <string> int main() { std::string line; // empty string while(std::getline(std::cin, line)) { 1. 2. 3. 4. 5. 6. 7. // read line at time until end-of-file std::cout << line << std::endl; // write s to the output ...
using namespace std; string s; int main() { cout<<"请输入一个含大写的字符串:"; string str; cin>>str; ///转小写 transform(str.begin(),str.end(),str.begin(),::tolower); cout<<"转化为小写后为:"<<str<<endl; transform(str.begin(),str.end(),str.begin(),::toupper); ...
{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>...
data() << std::endl; //移除,1,3两个位置的字符 std::cout << (QString("123456").remove(1,3)).toStdString().data() << std::endl; // 超过 11 个字符就保留 11 个字符,否则不足替换为 '.' std::cout << (QString("abcdefg").leftJustified(11,'.',true)).toStdString().data(...