1

this code should convert uppercase to lowercase and vice versa .. the problem is its returning both upper and lower .. for example when i enter the word "tuna" it returns "TUNAtuna" .. help please

string rev_letter(string s)
{
    string word = s;
    string final_word="";
    char c;
    for(int i = 0 ; i<=(word.length()-1);i++)
    {
    c=word.at(i);
    if(isupper(c))
    {
        putchar(tolower(c));
        final_word+=c;
    }
    else
    {
        putchar(toupper(c));
        final_word+=c;
    }

    }
    return final_word;
} 
4

1 回答 1

1

当您使用 时putchar,您会按预期打印字符(到屏幕上),但不会将字符放入final_word. 在 final_word 中,您实际上放置了初始单词。

尝试这个:

string rev_letter(string s)
{
    string word = s;
    string final_word="";
    char c;
    for(int i = 0 ; i<(word.length());i++)
    {
       c=word.at(i);
       if(isupper(c))
       {
           final_word+=tolower(c);
       }
       else
       {
           final_word+=toupper(c);
       }
    }

    return final_word;
} 
于 2013-09-17T10:11:54.583 回答