一、題目信息
字符串替換
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7501 Accepted: 3560
Description
編寫一個C程序實現將字符串中的所有"you"替換成"we"
Input
輸入包含多行數據
每行數據是一個字符串,長度不超過1000
數據以EOF結束
Output
對於輸入的每一行,輸出替換後的字符串
Sample Input
you are what you do
Sample Output
we are what we do
二、參考代碼
[cpp]
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str;
while(getline(cin,str))
{
string::size_type pos = 0;
while ( (pos = str.find("you", pos)) != string::npos ) {
str.replace( pos++, 3, "we" );
}
cout << str << endl;
}
return 0;
}