zoukankan      html  css  js  c++  java
  • 字符串的替换

    字符串替换

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

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






    #include<iostream>
    #include<stdio.h>
    #include<string>
    using namespace std;
    int main()
    {

    int n;
    cout<<"请输入实验次数"<<endl;
    cin>>n;
    while(n--)
    {
    string s=" ",s1;
    cout<<"这是第"<<n+1<<"次"<<endl;
    cin>>s1;
    while(s1!="#") //字符串以"#"键结束
    {
    cout<<"s1== "<<s1<<endl;
    s=s+" "+s1;
    cout<<"s== "<<s<<endl;
    cin>>s1;
    }
    cout<<"原版字符串 "<<s<<endl;
    int i=0;
    while(s.find("you",i)!=string::npos)    
    {
    i=s.find("you",i);             //找到单词you 的地方
    s.replace(i,3,"we");         //将you 替换成we
    }
    cout<<"改进过的字符串"<<s<<endl;

    }
    return 0;
    }



  • 相关阅读:
    家庭问题(family)
    BFS简单题记
    【例2-3】围圈报数
    【例8.3】最少步数
    【例3-5】扩展二叉树
    股票买卖
    小球(drop)
    用循环单链表实现约瑟夫环
    二叉树的3种遍历6种实现
    const与#define宏常量 , inline与#define
  • 原文地址:https://www.cnblogs.com/zdblog/p/3637609.html
Copyright © 2011-2022 走看看