converting strings to a mixture of alternating uppercase and lowercase letters

Hi ya all,

My prof gave me a HW to ask a user to type a sentence and convert it
to a mixture of upper case and lower case each letter.
So for example, a string such as “Hello how are you?”
would be “HeLlO HoW ArE YoU?”

He also said you must use a char array (c-string), declared inside of main (local, not global).
Here's what I've done so far.

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

using namespace std;

int main()
{
char mix_of_string[256];
cout << "Please enter any string.\nYour string will be converted\nto a mixture of\nalternating uppercase and lowercase letters. " << endl;
cin.get (mix_of_string, 256);
bool to_up = true;
for (int i = 0; i < strlen(mix_of_string); i++)
{
if (to_up)
{
mix_of_string[i] = toupper(mix_of_string[i]);
}
else
{
mix_of_string[i] = tolower(mix_of_string[i]);
}
}
cout << "Your converted string is: " << mix_of_string << endl;
system("pause");
return 0;
}

The problem is the output shows only upper case letters not both at the same
time. And plus, How do I make it to output a mixture of its letters?
HELP ME OUT PLZ!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cctype>

int main()
{
    const int SZ = 256 ;
    char mix_of_string[SZ];

    std::cout << "Please enter any string (max " << SZ-1 << ") characters.\nYour string will be"
                 " converted to a mixture of\nalternating upper case and lower case letters.\n" ;

    std::cin.get( mix_of_string, SZ ) ;

    bool to_up = true;
    // for each character, up to (not including) the terminating null character
    for( int i = 0; mix_of_string[i] != 0 ; ++i ) // mix_of_string[i] != 0 ; if this is not a null character
    {
        if(to_up) mix_of_string[i] = std::toupper( mix_of_string[i] );
        else mix_of_string[i] = std::tolower( mix_of_string[i] );

        to_up = !to_up ; // **** toggle the flag
    }

    std::cout << "Your converted string is: '" << mix_of_string << "'\n" ;
}
I did as you did and it beautifully works!! big thanks!!!
Topic archived. No new replies allowed.