changing numbers to upper case letters

Aug 3, 2014 at 9:34pm
Hi, I was wondering how I would change the numbers 0-9 to the upper case letters A-J. I dont want to have to write a switch statement or an if else for each number, I dont know how I would do it without it.


If a user input hi123
the output would be hiBCD
Aug 3, 2014 at 9:39pm
Alphabetic characters are stored numerically, and in order.
So are numeric characters.

Therefore you can do math to translate them.

IE:

1
2
3
4
5
6
7
8
9
10
11
12
char c = '5';  // <- c is the '5' character

c -= '0';  // subtract the '0' character.  Now, c is no longer the ASCII code for '5',
      // but rather, is the literal number 5
c += 'A';  // add our numerical 5 to the 'A' character... giving us the 'F' character

cout << c;  // prints 'F' as you'd expect


// or shorthand:

c += 'A' - '0';  // do the conversion in one step. 
Aug 4, 2014 at 1:01am
Alphabetic characters are stored numerically, and in order.
So are numeric characters.

This is true for practical purposes, but it isn't guaranteed to be true. We could take another approach that is guaranteed to work (and is a more general solution, although not quite so simple.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// http://ideone.com/xdropp
#include <iostream>
#include <string>
#include <cassert>
#include <utility>

struct translator
{
    translator(std::string src, std::string tgt) 
        : _source(std::move(src)), _target(std::move(tgt)) 
    {
        assert(_source.length() == _target.length());
    }


    char operator()(char ch) const;

private:
    std::string _source;
    std::string _target;
};


// if ch is found in _source, return the corresponding character in _target
// if it isn't found in _source, return ch
char translator::operator()(char ch) const
{
    auto pos = _source.find(ch);
    return pos == std::string::npos ? ch : _target[pos];
}

// helper function.  Applies translator to every element in a string.
std::string translate(const translator& xlate, std::string s)
{
    for (auto& ch : s)
        ch = xlate(ch);

    return s;
}


int main()
{
    translator xlator("0123456789", "ABCDEFGHIJ");

    std::string line;

    while (std::getline(std::cin, line) && !line.empty())
        std::cout << translate(xlator, line) << '\n';
}
Aug 4, 2014 at 5:27am
It is as simple as this small program if you take a single Alphanumeric string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

int main()
{
    string user_input;

    //Taking Input From User
    cout << "Enter a alphanumeric string: ";
    cin >> user_input;

    //Processing String
    for(int i=0;i<user_input.size();i++)
        if(user_input[i]>='0' && user_input[i]<='9')
            user_input[i] = user_input[i] + 17;     //17 = 65 - 48 b/c 0 + 65 = A and char '0' == 48

    cout << "Result: " << user_input;

    cout << endl;
    cin.ignore();
    return 0;
}


Hint: For These Type of Programs always make use of the ASCII Table :)
Last edited on Aug 4, 2014 at 5:31am
Aug 4, 2014 at 3:33pm
Hint: For These Type of Programs always make use of the ASCII Table :)


Or just use the ASCII characters in your program so you don't have to use an ASCII table and your code is easier to understand.

1
2
3
4
5
// instead of this:
user_input[i] = user_input[i] + 17;     //17 = 65 - 48 b/c 0 + 65 = A and char '0' == 48

// just do this:
user_input[i] = user_input[i] + 'A' - '0';
Topic archived. No new replies allowed.