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

求助,编译没问题,运行时打印 已放弃

xulihua 发布于 2008-09-18 13:54, 556 次点击
想在 大写字母前加一个空格(除第一个大写字母)

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
        int flag = 0;
        string str("XiaoYaoKe");
        string::iterator ite = str.begin();
        while(ite != str.end())
        {
                if(isupper(*ite))
                {
                        if(!flag)    //第一个大写字母不作处理
                                flag = 1;
                        else
                                str.insert(ite, ' ');
                }
                ite++;
        }
        cout << str << endl;
        return 0;
}
2 回复
#2
ma35872008-09-18 18:28
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
    int flag = 0;
    string str("XiaoYaoKe");
    string::iterator ite = str.begin();
    while(ite != str.end())
    {
        if(isupper(*ite))
        {
            if(!flag)   
                flag = 1;
            else
            {
                ite = str.insert(ite, ' '); //返回值是个iterator
                ite++; //ite要加1,否则总是判断同一个字符
            }
        }
        ite++;
    }
    cout << str << endl;
    return 0;
}

[[it] 本帖最后由 ma3587 于 2008-9-18 18:29 编辑 [/it]]
#3
xulihua2008-09-19 16:27
谢谢
试过了,OK~
1