注册 登录
编程论坛 C++教室

程序问题

suckdog 发布于 2007-12-02 04:04, 898 次点击
有一条题目问你输入一行英文, 比如说“See an adviser, talk to him, and listen to him."
现在要把这句话里所有的him 替换成him/her, 请大家帮忙看看,谢谢

需要用get string来输入句子, 例如 cstr[20]="see an adviser........";
6 回复
#2
suckdog2007-12-03 04:40
有人会吗???
#3
xprince2007-12-03 04:50
先遍历字符数组
替换掉你的替换的字符再更新输出不就行了
#4
随心2007-12-03 09:18
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
        vector<string> svec;
        string word;
        while (cin >> word)
                svec.push_back(word);
        for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
        {
                if (*iter == "him")
                        *iter = "him/her";
                cout << *iter << ends;
        }
        putchar('\n');
        system("pause");
        return 0;
}
#5
suckdog2007-12-03 13:27
谁用borland C++编这个程序
#6
aipb20072007-12-03 15:42
原帖由 [bold][underline]suckdog[/underline][/bold] 于 2007-12-3 13:27 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1128200&ptid=189827][/url]
谁用borland C++编这个程序


borland c++难道不是c++?
#7
csmenglei9512007-12-04 18:20
#include <iostream>
#include<cstring>
using namespace std;
const int size = 100;
void replace( char *p );
int main()
{
    char bereplace[size];
    cout << "please input the strings you want to edit!" << endl;
    cin.getline( bereplace, size );
    for ( int i = 0; i < strlen(bereplace); i ++ )
    
        replace( &bereplace[i] );
    cout << bereplace << endl;
    return 0;
}
void replace ( char *p )
{
    if ( *p == 'h' && *(p+1) == 'i' &&*(p+2) == 'm' )
    {
        *(p+1) = 'e';
        *(p+2) = 'r';
    }
}
1