c++,输入一串字符 以回车结束 用一串字符替换其中的一个字母程序运行第

c++,输入一串字符 以回车结束 用一串字符替换其中的一个字母程序运行第一行显示输入的字符串 第二行显示被替换的字母 第三行显示替换的字符串

//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using namespace std;
int main(void){
    string str,s;
    char ch;
    int t,f=0;
    cout << "Please enter a string...\nstr=";
    cin >> str;
    cout << "Please enter the characters to be replaced...\nch=";
    cin >> ch;
    while(1){
        cout << "Please enter a string to replace...\ns=";
        if(cin >> s && s.find(ch)==string::npos)
            break;
        cout << "Error(there a '" << ch << "' in s, redo: ";
    }
    while((t=str.find(ch))!=string::npos){
        str.replace(t,1,s);
        f++;
    }
    if(f)
        cout << "Replace " << f << " '" << ch << '\'' << endl;
    else
        cout << "Could not find the '" << ch << '\'' << endl;
    cout << str << endl;
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-07-14
没一个标点符号,差评
相似回答