I wrote this code to change all lowercase letters in a string to uppercase. That works, but I also want it to go the other way around and nothing I tried is working.

#include<iostream> 
#include<string>
#include<cctype>

using namespace std;

string change();

int main()
{
    change();
    return 0;
}        

string change()
{
    cout << "Enter a string: ";
    string s;
    getline(cin,s);

    int i=0;

    while(i<s.length())
    {
        s.at(i)=toupper(s.at(i));
        i++;    
    }

    cout << s << endl;

    return s;
}

By "go the other way" you mean you use s.at(i) = tolower(s.at(i))?

yes.

You could also use -

void TransformUppercase(std::string&  s)
{
    std::transform( s.begin(),
                    s.end(),
                    s.begin(),
                    ((int(*)(int))std::toupper));
    return;
}

I learned about passing by reference, but I never learned about s.begin and s.end so I'm not sure I can use that.

Here I'm pretty sure you can use these.. I wrote them myself a while ago while writing an include for bitmap finding.. Usually we don't actually give out code but I can see that you tried and plus this code isn't complicated so I'm sure it's ok for me to share.

Also where it shows comments.. you can use those instead of the ToUpper.. but I just used ToUpper instead of using the Ascii table. Does uppercase, lowercase and capitalization. Enjoy. Note: If you uncomment the ascii table ones, you won't need any additional includes..

string UpperCase(string Str)
{
    /**
for (unsigned short I = 0; I < Str.size(); I++)
if(Str[I] >= 0x61 && Str[I] <= 0x7A)
Str[I] = Str[I] - 0x20;
**/
    //Str[I] -= 'a' - 'A' ;
    for (unsigned short I = 0; I < Str.length(); I++)
    {
        Str[I] = toupper(Str[I]);
    }
    return Str;
}

string LowerCase(string Str)
{
    /**
for (unsigned short I = 0; I < Str.size(); I++)
if(Str[I] >= 0x41 && Str[I] <= 0x5A)
Str[I] = Str[I] + 0x20;
**/
    //Str[I] += 'a' - 'A' ;
    for (unsigned short I = 0; I < Str.length(); I++)
    {
        Str[I] = tolower(Str[I]);
    }
    return Str;
}

string Capital(string Str)
{
    Str[0] = toupper(Str[0]);
    return Str;
}

so I need separate functions for each?

Welll you can of course edit it yourself.. Add a boolean parameter.. If true, Lowercase.. If false, Uppercase.. It's up to you to decide how your going to use it.. you can probably just copy the code inbetween it if you do not want a function..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.