1.读一个字符串,把所有的“you”替换成“we”
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
int pos;
while (getline(cin,str)) {
while ( (pos=str.find("you"))!=-1 ) {
str.replace(pos, 3, "we");
}
cout<<str<<endl;
}
}
2.c++中substr的用法
#include<string>
#include<iostream>
using namespace std;
void main()
{
string s("12345asdf");
string a=s.substr(0,5);
cout<<a<<endl;
}
上述代码获得字符串s中 从第0位开始的长度为5的字符串.默认时的长度为从开始位置到尾
输出结果为:
12345