Changing a lowercase character to uppercase in c++
Asked Answered
H

6

10

Here is the code that i wrote. When i enter a lowercase character such as 'a', it gives me a blank character but afterwards it works well. Can you tell me what i did wrong? Thanks. :)

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char letter;

    cout << "You will be asked to enter a character.";
    cout << "\nIf it is a lowercase character, it will be converted to uppercase.";
    cout << "\n\nEnter a character. Press . to stop: ";

    cin >> letter;

    if(islower(letter))
    {
        letter = isupper(letter);
        cout << letter;
    }

    while(letter != '.')
    {
        cout << "\n\nEnter a character. Press . to stop: ";
        cin >> letter;

        if(islower(letter))
        {
            letter = toupper(letter);
            cout << letter;
        }
    }

    return 0;
}
Hyperbaric answered 15/3, 2014 at 15:32 Comment(4)
Because you print a bool value in the first time.Gumma
What do you expect this to do? letter = isupper(letter); cout << letter;. What can isupper return, and what does that correspond to in, say , ASCII?Grease
Can you tell me how i should modify my code please?Hyperbaric
Thanks everybody that's a silly mistake that i did. I thought i modified isupper() to toupper() for both parts. Lol Thanks. :)Hyperbaric
G
13

Because you print a bool value (i.e. false, aka, NUL character here) in the first time.

You should change

letter = isupper(letter);

to

letter = toupper(letter);
Gumma answered 15/3, 2014 at 15:37 Comment(0)
T
2

Look here:

if(islower(letter))
{
    letter = isupper(letter);
    cout << letter;
}

If the character is lower, then you assigned it the return value of isupper. That should be 0. So you print a null character.

Why don't you just call toupper for every character that you enter? If it's lower it will convert it, if it is already upper it won't do anything.

Tylertylosis answered 15/3, 2014 at 15:37 Comment(1)
Oh wow i didn't think of it that way lol. Thanks to everyone. :)Hyperbaric
S
1

Generally speaking to convert a lowercase character to an uppercase, you only need to subtract 32 from the lowercase character as this number is the ASCII code difference between uppercase and lowercase characters, e.g., 'a'-'A'=97-67=32.

char c = 'b';
c -= 32; // c is now 'B'
printf("c=%c\n", c);

Another easy way would be to first map the lowercase character to an offset within the range of English alphabets 0-25 i.e. 'a' is index '0' and 'z' is index '25' inclusive and then remap it to an uppercase character.

char c = 'b';
c = c - 'a' + 'A'; // c is now 'B'
printf("c=%c\n", c);
Spice answered 25/5, 2020 at 15:17 Comment(0)
T
0

In case you want you own algorithm:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    char ch = '/0';
    string input("Hello, How Are You ?");
    for(size_t i=0; i<input.length(); i++)
    {
        if(input[i]>=97 && input[i]<=122)
        {
            ch=input[i]-32;
        }
        else
        {
            ch = input[i];
        }
        cout << ch;
    }

  return 0;
}
Terramycin answered 19/9, 2018 at 5:22 Comment(0)
P
0

In letter = isupper(letter);, you check whether the variable letter is capitalized, to convert it to uppercase you must use the function toupper()

letter = toupper(letter);
Peach answered 22/6, 2021 at 13:14 Comment(1)
What value does your answer add over the other answers?Groves
B
0
#include <iostream>
using namespace std;

main()
{
    string name ;
    cout<<"Lower Case:  ";
    cin>>name;
    int  size_of_string = name.size(); // to detrmine the looping times
    cout<<"Upper case:  ";

    for(int i = 0 ,n = size_of_string ; i < n ; i++)
    {
        //check if the str[i] is lowercase 
        if(name[i]  >= 'a' && name[i]<='z' )
        {
            //convert the Lowercase to uppercase via ASCI Chart
            char a = name[i] - 32 ;
            cout<<a;
        }

        //the user input was upperCase 
        else
        cout<<name[i];
    }

    cout<<"\n Good Bye!"<<endl;
    return 0;
}
Biggs answered 6/7, 2021 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.