我和你
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
将一句话中的所有的“我”换成“你”,“你”换成“我”。
-
输入
-
多组测试数据
每组给出一句中文,每段少于100个汉字
以单独一个0结束
输出
- 对于每组测试数据,输出替换后的句子。 样例输入
-
我爱你 我是中国人 1234我 0
样例输出
-
你爱我 你是中国人 1234你
提示
注意:每个中文在windows下占两个字节,ubuntu默认使用UTF-8编码,每个中文占三个字节。本OJ判题环境为ubuntu
代码实现:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str,a="我";
int len=a.size();
while(cin>>str,str!="0")
{
for(int i=0;i<str.size();i++)
{
if(str.substr(i,len)=="我")
str.replace(i,len,"你");
else if(str.substr(i,len)=="你")
str.replace(i,len,"我");
}
cout<<str<<endl;
}
return 0;
}
使用C++中的查找字串函数str.substr()函数和字串替代函数str.replace()来求解本题。从此题中我又多了解些C++中对于字符串的操作
-
多组测试数据