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. :)

c++

Recommended Answers

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 …
Jump to Post

All 4 Replies

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';

This is the output. :(

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]);
Be a part of the DaniWeb community

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