在此利用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为:...
std::string s = "hello world"; std::cout<<s<<std::endl; for (std::string::size_type ix = 0; ix != s.size(); ++ix) s[ix] = '*'; std::cout<<"Now s is:"<<s<<std::endl; std::cout<<"s's len is:"<<s.size()<<", s[12]="<<s[100]<<std::endl; return 0...
return 0; } 运行结果: 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;} ...
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); ...
char string[]="THIS IS A STRING";length=strlen(string);printf("%s\n",string);for(i=0;i<length;i++){string[i] = tolower(string[i]); } printf("%s\n",string);system("pause"); } c++程序例:include <iostream> include <string> include <cctype> using namespace std;int ...
1 #include <string> 2 using namespace std; string对象的输入方式: cin\getline 1 #include <iostream> 2 #include <string> 3 4 int main() 5 { 6 string s1, s2; 7 cin >> s1; 8 getline(cin, s2); 9 10 return 0; 11 } 二、C字符串相关操作 ...
(unsigned char c) return std::tolower(c); }); return normalized; } // 查找并替换文档中的单词 void findAndReplaceWords(std::istream& input, std::ostream& output, const std::string& from, const std::string& to) { std::string line; while (std::getline(input, line)) { std::...