YoungA1
YoungA1

Reputation: 53

Converting input from lowercase to uppercase using ASCII codes in C++

This is my first question here, so I've done my best to make this a good question.

I am creating a program that essentially takes user input and converts all characters to uppercase. I am using a for-loop to scan for lowercase characters using corresponding ASCII codes.

I am able to do this just fine using a character array that is assigned a string in-code: char text[] = "Text".

I want to be able to take user input and use it in the character array. I tried using getline(cin,myString) and assigning the character array with that, but it says array must be initialized with a brace enclosed initializer.

I kept the character array uninitialized because sizeof(text) isn't giving the correct size when the array is initialized. I was reading about using pointers but I'm still a bit fresh on that topic. Below is the code I wrote:

int main() {
    // User input as a string
    char textConvert[] = "This text will be converted to uppercase.";
    cout << textConvert << endl;
    int endChar = sizeof(textConvert); //Only gives correct size when array is uninitialized
    for (int i = 0; i < endChar; i++) {
        if (textConvert[i] >= 97 && textConvert[i] <= 122) {
            textConvert[i] = textConvert[i] - 32;
        }
    }
    cout << textConvert;
    return 0;
}

Upvotes: 0

Views: 7034

Answers (2)

Loki Astari
Loki Astari

Reputation: 264381

Question:

I tried using getline(cin,myString) and assigning the character array with that, but it says array must be initialized with a brace enclosed initializer

Here the compiler works out the size of the array needed.

    char textConvert[] = "This text will be converted to uppercase.";

If you want user input you need to allocate an array and specify size.

    char textConvert[50];

Now you can read a line and copy it into the array:

    std::string myString;
    std::getline(std::cin , myString);
    // Should check that the string is not more than 50 characters.
    std::copy(std::begin(myString), std::end(myString), textConvert);

But really there is no need to do this at all. Just use the std::string and loop over the string. Best to avoid C constructs like arrays and use the C++ constructs that stop you making errors.

Size of String

This is not a good idea.

    int endChar = sizeof(textConvert);

This measures the size of the array (not the size of the string). There is also an issue that arrays will very easily decay into pointers. When this happens sizeof() will give you the size of the pointer (probably 4 or 8) not the size of the array.

To get the size of a string use std::strlen() (include <cstring>).

But really you should be using std::string the C++ version of string that does its own memory management and re-sizes as required.

Magic Numbers

Prefer not to use magic numbers:

        if (textConvert[i] >= 97 && textConvert[i] <= 122) {
            textConvert[i] = textConvert[i] - 32;
        }

These magic numbers make the code hard to read. You can use character constants instead.

        if (textConvert[i] >= 'a' && textConvert[i] <= 'z') {
            textConvert[i] = textConvert[i] - ('a' - 'A');
        }

Prefer the standard Library

But doing this manually is not recommended. You should use the standard library routines.

std::islower() .  // Check if a character is lower case.
std::toupper() .  // Convert a lowercase character to upper.

// include <cctype>

C++ Example

Try this:

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

int main()
{
    std::string   myString;
    while(std::getline(std::cin, myString)) {
        std::cout << "User Input: " << myString << "\n";
        for(auto& c: myString) {
            c = std::toupper(c);
        }
        std::cout << "Upper Case: " << myString << "\n";
    }
}

Upvotes: 1

Jesper Juhl
Jesper Juhl

Reputation: 31467

Since you are dealing with ASCII, you can just use std::toupper.

No need to write custom code to do it, the standard library has you covered.

Upvotes: 0

Related Questions