0

I am trying to make an upper case key and a lower case key of the one typed by the user but when I use the toupper and tolower functions they turn all my chars into lower or upper. Not just the one I want. Valkey is the key the user typed. If they type VBnmdG, I get vbNMDg for all three strings instead of the original untouched, the upper in all uppers and the lower in all lowers.

string key_upper = valkey;
string key_lower = valkey;
for (int i = 0; i < len; i++)
{
    if (isalpha(valkey[i]) == 0)
    {
        printf("Key must only contain alphabetic characters.\n");
        return 1;
    }
    if (isupper(valkey[i]))
    {
        key_lower[i] = tolower(key_lower[i]);
    }
    else if (islower(valkey[i]))
    {
        key_upper[i] = toupper(key_upper[i]);
    }
    else
        continue;
}

printf("valkey: %s\n", valkey);
printf("keyupper: %s\n", key_upper);
printf("keylower: %s\n", key_lower);
0

Your problem is what you exactly told the program to do, you said if it is a lowercase char you turn it into uppercase and vice versa.

Also your else statement and continue can be omitted since you're not really doing anything after those making it useless.

0

I was declaring the variables where I wanted to store de upper and lower keys by copying the inserted key. All three variables were using the same memory space and overwrote each other. I changed:

string key_upper = valkey;

string key_lower = valkey;

and instead I put this to declare the variables:

char key_upper[26];

char key_lower[26];

and it worked!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .