Can anybody help me with my program? I can't get the example. Here's the question: Create a program that accepts an array of characters. And displays the converted array of characters into Upper case if it is given in
Lowercase and vice versa. Don't use string, but char.

Example: mychar ="I am A conQUeror."

//My Codes:
#include <iostream>
#include <conio.h>
using namespace std;

int main() 
{
cout << "Password? \n";
char input[256];
cin.get(input, 256);

for(int i = 0; i < strlen(input); i++)
{
        input[i] = toupper(input[i]);
        cout << endl << input << endl;
        }

for(int i = 0; i < strlen(input); i++)
{
        input[i] = tolower(input[i]);
        cout << endl << input << endl;
        }

getch ();
return 0;
}

Thank you. :)

The logic should be conditional. You're inverting the case based on the original case, not unconditionally changing it:

for (int i = 0; input[i] != '\0'; i++)
{
    if (isupper(input[i]))
    {
        input[i] = tolower(input[i]);
    }
    else if (islower(input[i]))
    {
        input[i] = toupper(input[i]);
    }
}

cout << input << '\n';

Works fine for me. Compare and contrast:

#include <iostream>

using namespace std;

int main() 
{
    char input[256];

    cout << "Password? ";
    cin.get(input, 256);

    for (int i = 0; input[i] != '\0'; i++)
    {
        if (isupper(input[i]))
        {
            input[i] = tolower(input[i]);
        }
        else if (islower(input[i]))
        {
            input[i] = toupper(input[i]);
        }
    }

    cout << input << '\n';
}

You could define the following function:

char invert(char c)
{
  return isupper(c)? tolower(c) : toupper(c);
}

and then use like this:

for(int i=0; input[i] != '\0'; i++) input[i]=invert(input[i]);
This article has been dead for over six months. Start a new discussion instead.