Convert letters to int values

Feb 10, 2012 at 8:28pm
Just got done with learning about for loops and functions in my intro to C++ class and I'm doing well with all the simple tasks our professor has us doing. Its really fun and rewarding making stuff. I decided to work on a little project of my own which I thought I'd be able to handle but I might have bitten off more than I can chew.

Essentially the user is asked to enter some text like Hello or HELLO and I need to assign an integer value to each character so that A or a = 0 and Z or z = 25. So hello would translate to 7-4-11-11-14. Which I'd then have to add to some other user inputted text to Form some new int values which would then translate back to some text.

Perhaps some of you already have an idea of what it is i'm trying to do. Point is I need to figure out how to turn letters to numbers and vice versa.

Something I thought off doing but tells me is just wrong is this.

char A='A';
int(A)=0;
char B='B';
int(B)=1;
char C='C';
int(C)=2;
and so on...


I read online about using arrays but we have yet to get to that in my class and I'm only using what I know.


Help?
Feb 10, 2012 at 8:43pm
Each character you are using already has a decimal and hex equivalent to the ascii character it represents. See this for a reference http://web.cs.mun.ca/~michael/c/ascii-table.html.
Its really fun and rewarding making stuff
I'm glad your having fun with this. What I am suggesting is find out the relationship between the decimal values of your characters in upper and lower case. You already know what you want them to be converted to 0 - 25, now find the offset from the table I have given you and which a function that does the conversion to decimal. You can write another function that converts the decimal back to a character. Just try and make it work for all upper case first (if doing both upper and lower is too much), then make it work for lowercase. After you have it working, see if there is a way to make it more elegant. You definitely don't want to type out the entire alphabet twice and you don't have to.
Feb 10, 2012 at 9:24pm
heh

char const basechar('a');
int const val(givenchar - basechar);
Feb 12, 2012 at 5:12pm
so using the above knowledge and a simple for loop I've managed to output from 'A'-'Z' 0-25.

Now i'm just confused if those are associated with it and if not how to get them to. Still thinking about it. I'm hoping after my my class goes over Arrays I'll have a better understanding on implementing this. Cause I still need it to recognize a string of letters and not just an individual letter.

Thanks for the replies.

EDIT: So it seems like letter by letter it works. I just need to convert the number I get from the first two letters into the letter that number represents.

After that I need to see how to get it to work with a series of letters (a string?) and not just letter by letter.

EDIT: Ok Awesome so its doing exactly what I want it to do letter by letter for capital letters thus far. Now to figure out for it to do it with more than one letter. When I try using an array I just get gibberish for every word I enter. :/
Last edited on Feb 12, 2012 at 6:34pm
Topic archived. No new replies allowed.