1

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!

5
  • Do you want an sequence of ints or of chars? Commented Jun 22, 2015 at 2:39
  • You need a separate index variable for writing to key_num e.g. key_num[j++], otherwise you will have empty spaces in key_num where isalpha(key_char[i]) is false Commented Jun 22, 2015 at 2:41
  • Hi there, I want a sequence of ints. For instance, if the users input "abz", I want my output string to be "0125" Commented Jun 22, 2015 at 2:41
  • Hi samgak, I am relatively new to programming, my understanding from your comment is that you can't share a index variable; in this case, these two strings. I don't quite understand why would isalpha would be false, do you mind explain a bit further! Commented Jun 22, 2015 at 2:47
  • isalpha would be false when the value isn't a letter , for example: '?' or '9' Commented Jun 22, 2015 at 2:50

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I am new to programming , and the concepts you have covered were a bit too advanced for me. I will definitely learn how vector works, thank you quite much!
A vector is a sequence that can grow. The four loop here is called a range-based for loop in c++, but is known more commonly as a for-each loop on other languages. The idea is just to say "for each element in some_sequence" rather than a loop that just counts
Thank you Ryan! That made a whole lot of sense!
2

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

Comments

2

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.