Give letters specific numbers.

Jan 19, 2011 at 11:41pm
Hi
Before I say anything, I think that you must know that I am very new to c++. I have not worked with it a long time.
So...
I try to do a program that will change each letter in a text to a specific number.
My though is something like this

A=1
B=2
C=3
D=4
And so on...

then you enter a text, for example "Hi my name....." will show as "8,9, 13,25, 14,1,13,5,......"

I am not totally dumb and I think I will understand if some of you guys can explain whats the best way I should go if I do this kind of program.

//Soprot
Jan 19, 2011 at 11:44pm
closed account (3hM2Nwbp)
There's more than one way to skin this cat. You can either have a gargantuan switch (don't do it), or subtract a constant amount from the letters' ASCII values. You can search Google for an ASCII table to reference.

*Edit - consider also using the to_upper or to_lower standard methods to simplify your input.

*Edit2 - You can also do it by shifting some bits. :)
Last edited on Jan 19, 2011 at 11:47pm
Jan 20, 2011 at 12:06am
Another way is to make a loop and assign each letter to a integir value as they increment
Last edited on Jan 20, 2011 at 12:07am
Jan 20, 2011 at 11:20am
closed account (z05DSL3A)
You could use a std::map, and for each char find the coresponding int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    map<char,int> code;
    map<char,int>::iterator code_itr;
    for(int i = 0; i < 26; ++i)
    {
        code['a'+i] = i + 1;
    }

    string msg = "hello world!";
    for(string::iterator str_itr = msg.begin(); str_itr != msg.end(); ++str_itr)
    {
        if( (code_itr = code.find(*str_itr)) == code.end() )
            cout << *str_itr;
        else 
            cout << (*code_itr).second;
        cout << " ";
    }
    cout << endl;

Jan 20, 2011 at 12:07pm
But if you are concerned that the underlying character set is not alphabetically contiguous, you'll need a specific lookup:
http://stackoverflow.com/questions/1616706/how-to-get-characters-position-in-alphabet-in-c-language
Jan 20, 2011 at 12:12pm
I'd probably go with Luc Lieber's solution.

In case you don't already know this soprot, all characters are already essentially just numbers, as each has a unique ASCII value. That wasn't really explained above. So as Luc Lieber said, all you have to do is use the topuuer function on a char variable containing a letter, then subtract 64 to get a number between 1 and 26
Jan 20, 2011 at 12:14pm
Hi Duoas,

I don't know much about ASCII, when would a character set not be alphabetically contiguous? Is ASCII not the standard chacters set?
Jan 20, 2011 at 11:18pm
Thanks very much. I actually made it trough. my programis almost done, but now im facing a new problem. How do i copy text from my running program (batch file) and paste it into another program (also running as a batch). ??
Jan 22, 2011 at 4:25pm
sorry, my prob was easy to solve. Sorry for my dumbness.
Topic archived. No new replies allowed.