zoukankan      html  css  js  c++  java
  • 字符串替换 string (stl) find + replace

    题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=113

    字符串替换

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:2
     
    描述
    编写一个程序实现将字符串中的所有"you"替换成"we"
     
    输入
    输入包含多行数据 

    每行数据是一个字符串,长度不超过1000 
    数据以EOF结束
    输出
    对于输入的每一行,输出替换后的字符串
    样例输入
    you are what you do
    样例输出
    we are what we do

    分析:
    1:find(string&s, location) , 从 location 位置开始,在字符串中找子串s, 找到返回找到的第一个首字符位置,否则返回 string::npos。

    2:replace(location, size, string &s) , 从location 位置开始, 数 size 个 数的 字符串, 用 字符串s 代替。
    3: cin>>string s , 遇到空白结束输入 。 如果想输入一行, 则用 getline(cin, S).

    代码如下:
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <vector>
    #include<string>
    #include<cstring>
    #include<string.h>
    #include<set>
    #include<queue>
    #include<stack>
    #include<map>
    using namespace std;
    typedef long long LL;
    
    int main()
    {
        string s;
    
        while(getline(cin,s)) // 一行输入
        {
            int loc=s.find("you");
            while(loc != string::npos)      // 查找
            {
                s.replace(loc,3,"we");
                loc=s.find("you",loc+1);
            }
            cout<<s<<endl; // 一行输出
        }
        return 0;
    }
     
  • 相关阅读:
    session监听
    Ubuntu上安装MongoDB(转)
    JAVA中的集合(转)
    Iterator的用法(转)
    PHPExcel常用方法汇总(转)
    MongoDB的安装及在PHP中的配置Windows版
    [转载]使用FastReport 3.0及以上版本创建动态报表的几个技巧
    FastReport
    ZeosLib
    [转载]FastReport问题整理
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3621006.html
Copyright © 2011-2022 走看看