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

    将文本文件中指定的字符串替换成新字符串。 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。

    输入格式:

    Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

    end (表示结束)

    Institute (第一个字符串,要求用第二个字符串替换)

    University (第二个字符串)

    输出格式:

    Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

    输入样例:

    Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.

    The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

    end

    Institute

    University

    输出样例:

    Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

    解题:利用find函数和replace函数

    find函数
    1.stringfind()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回-1
    2.查找某一给定位置后的子串的位置。例如str.find("bad",5),从母串下标为5开始搜索bad的位置,如果存在返回bad在母串的下标否则返回-1
    3.查找所有子串在母串中出现的位置(此题就是如此)
    模板:
    int found=str.find("bad",0);

    while(found!=-1)
    {
    cout<<found<<endl;//输出bad在母串的位置
    found=str.find("bad",found+1);
    }
    4.反向查找子串在母串中出现的位置,通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一。
    str.rfind("bad");
    replace函数
    replace最常用的形式是str.replace(found,length,c)//在字符串str中从found位置开始用字符串c替换总长为length的字符串

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
    string a,b,c,m;
    getline(cin,a);
    while(1)
    {
    getline(cin,m);
    if(m=="end"){
        break;
    }    
    a+='
    ';
    a+=m;    
    }
    a+='
    ';
    getline(cin,b);
    getline(cin,c);
    int found;
    found=a.find(b);
    while(found!=-1)
    {
    a.replace(found,b.size(),c);
    found=a.find(b,found+1);    
    }cout<<a; 
    return 0;
    } 
    不一样的烟火
  • 相关阅读:
    (转载)gethostbyname() 用域名或主机名获取IP地址
    (转载)Linux网络编程inet_aton和inet_network和inet_addr三者比较
    (转载)Linux网络编程inet_pton与inet_ntop
    (转载)获取以太网卡的MAC地址
    (转载)Linux网络编程使用gethostbyaddr,通过ip地址,得到该ip地址的主机的信息
    (转载)Linux网络编程inet_ntoa问题记录
    Linux中pthread_detach()线程注意
    regexpfield tcl tk
    Iwidgets checkbox
    iwidgets hyperhelp tcl tk
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11115825.html
Copyright © 2011-2022 走看看