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

请高手帮个忙,小写改为大写,怎么改不了

xulihua 发布于 2008-09-20 09:59, 638 次点击
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

string &fun(string &);
int main(void)
{
        string str("xiaoyaoke");
        cout << fun(str) << endl;
        return 0;
}
string &fun(string &s)
{
        string::iterator ite = s.begin();
        while(ite != s.end())
        {
                toupper(*ite++);
        }
        return s;
}
3 回复
#2
ma35872008-09-20 11:35
string &fun(string &s)
{
    string::iterator ite = s.begin();
    while(ite != s.end())
    {
        *ite++ = toupper(*ite);  //这样改
    }
    return s;
}
#3
xulihua2008-09-22 09:45
谢谢
我明白了,tolower(),toupper()返回字符,书上讲过了,我没注意~~
谢谢了!
#4
xulihua2008-09-22 09:55
*ite++ = toupper(*ite);
改为:*ite = toupper(*ite);
       ite++;
否则,toupper(*ite)返回值 赋值给 本应被赋值的字符的下一个字符了,从而导致第一个字母不输出的情况。
1