【南理oj】113 - 字符串替换(STL - string)
来源:互联网 发布:windows一直在检查更新 编辑:程序博客网 时间:2024/06/07 05:28
点击打开题目
字符串替换
时间限制:3000 ms | 内存限制:65535 KB
难度:2
- 描述
- 编写一个程序实现将字符串中的所有"you"替换成"we"
- 输入
- 输入包含多行数据
每行数据是一个字符串,长度不超过1000
数据以EOF结束 - 输出
- 对于输入的每一行,输出替换后的字符串
- 样例输入
you are what you do
- 样例输出
we are what we do
- 来源
- 水题比赛
- 上传者
- hzyqazasdf
这道题有两种思路:
①从原字符串找 " you " ,在原位置上替换为 " we "。最后输出。
②不断把原字符串赋值给 ans 字符串,若遇到 " you " 则赋值 " we "。最后输出ans字符串。
代码如下:
①
#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main(){string a;int loc;//"you"的位置 while (getline(cin,a)){loc = a.find("you",0);while (loc != string::npos)//查找失败返回string::npos的值 {a.replace(loc,3,"we");loc = a.find("you",0);}cout << a << endl;a.clear();}return 0;}
②
#include <cstdio>#include <string>#include <iostream> #include <algorithm>using namespace std;int main(){string a;while (getline(cin,a)){int l = a.size();string ans = "";for (int i = 0 ; i < l ;){if (i + 3 < l && a.substr(i,3) == "you"){ans += "we";i += 3;}else{ans += a[i];i++;}}a.clear();cout << ans << endl;}return 0;}
0 0
- 【南理oj】113 - 字符串替换(STL - string)
- NY--113 -- 字符串替换 [STL] [String]
- 字符串替换 string (stl) find + replace
- stl string截空格,替换字符串
- 【南理oj】1112 - 求次数(STL - map & string)
- 【南理oj】991 - Registration system(STL - map & string)
- NYOJ 113--字符串替换【string】
- NYOJ 113 字符串替换(C++STL解法)
- 南阳oj 题目113 字符串替换
- 南阳oj 字符串替换
- oj练习--字符串替换
- 字符串替换 STL
- string替换所有指定字符串(C++)
- string替换所有指定字符串(C++)
- string替换所有指定字符串(C++)
- string替换所有指定字符串(C++)
- string替换所有指定字符串(C++)
- string替换所有指定字符串(C++)
- iOS开发小技巧之--WeakSelf宏的进化,ios--weakself
- Unable to read entire header; 336 bytes read; expected 512 bytes
- Linux Centos-6 Mysql修改密码 不需要输入原密码
- LinearLayout布局中将一个控件放在屏幕的最下方-Android
- Poj 3259 Wormholes【SPFA判断负权回路】
- 【南理oj】113 - 字符串替换(STL - string)
- adb 无线连接手机
- BDL程序搬迁环境应注意的问题
- 我曾遇到的R文件报错的原因之一
- Linux迁移tomcat所在目录路径
- Fragment与Activity通讯
- 为RecyclerView的不同item项实现不同的布局(添加分类Header)
- 格雷码与二进制的转换
- 页面生成表格方式总结