Yu Zhou
Yu Zhou

Reputation: 173

Translating a character array into a integer string in C++

I was trying to achieve translating a character array into a integer string and corresponding character to their alphabetical order. For instance: A(a) = 0 , Z(z) = 25.

string key_char = argv[1];
string key_num;


    for (int i = 0; i < key_char.length(); i++){
        if (isalpha(key_char[i])){
            if (islower(key_char[i])){
                key_num[i] = int(key_char[i] - 97); //97 is the ascii value for 'a'
            }
            else{
                key_num[i] = int(key_char[i] - 65); // 65 is the ascii value for 'A'
            }

        cout << key_num << endl;

        }

My thought was to create two strings, key_char was to store command line input from the users in characters. key_num was to store the integer string after translation. I was thinking iterating through a loop and converting each character from the character array to the integer string through casting. I didn't receive any compiling errors, and it returned nothing either. Please let me know whether the idea of casting is viable in this situation or if there's a better solution to this matter. Thank you very much!

Upvotes: 1

Views: 76

Answers (3)

Ryan Haining
Ryan Haining

Reputation: 36792

If you want a sequence of int, then use a vector<int>. Using the key_char string, the values of the chars in it will serve as the initial value of the ints.

std::vector<int> key_num(key_char.begin(), key_char.end());

Then, iterate over each character of key_num and convert it to the equivalent int value

for (auto&& n : key_num) {
    n = std::toupper(n) - 'A';
}

or if you're in pre-c++11

for (std::vector<int>::iterator it = key_num.begin(), end = key_num.end();
        it != end;
        ++it) {
    *it = std::toupper(*it) - 'A';
}

After that you can print the ints

for (auto i : key_num) {
    std::cout << i;
}

You'll need to #include <cctype> and #include <vector> for this solution.

Your current approach is odd, the biggest problem is that the key_num you start with is an empty string, so you can't actually assign into it without fear of crashing.

Upvotes: 3

timrau
timrau

Reputation: 23058

You could use an std::ostringstream to construct the output string.

#include <sstream>
std::ostringstream strm;
for (int i = 0; i < key_char.length(); i++){
    if (isalpha(key_char[i])){
        if (islower(key_char[i])){
            strm << int(key_char[i] - 'a');
        }
        else{
            strm << int(key_char[i] - 'A');
        }
    }
}
key_num = strm.str();
std::cout << key_num << std::endl;

Upvotes: 2

UmNyobe
UmNyobe

Reputation: 22890

You can use directly literals 'a', 'A', because they are integers. Furthermore, key_char[i] - 'a' will evaluate to an integer, so no casting required.

If all you want is printing out you dont need an additional array.In the loop use

cout << (islower(key_char[i]) ? key_char[i] - 'a' : key_char[i] -'A')

and then

cout << std::endl 

after the loop

Upvotes: 2

Related Questions