# converting letters to numbers

Page 1 of 1

## 6 Replies - 2492 Views - Last Post: 10 November 2009 - 11:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=138088&amp;s=653cdaef73a3aa8471c1b1b840980b4b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 yoshiman

Reputation: 0
• Posts: 53
• Joined: 13-October 09

# converting letters to numbers

Posted 10 November 2009 - 11:13 AM

Ok, my teacher said that c++ automatically converts letters to numbers. such as(a=1,c=3, and z=26), but does that mean that I can add char variables together and get an int answer?

```char letter_one='a';
char letter_two='b';
int sum;

letter_one+letter_two=sum;

cout<<sum;
```

would the output be "3"?

if not would you mind telling me how I could get this to work?

This post has been edited by yoshiman: 10 November 2009 - 11:14 AM

Is This A Good Question/Topic? 0

## Replies To: converting letters to numbers

### #2 Anarion

• The Persian Coder

Reputation: 387
• Posts: 1,663
• Joined: 16-May 09

## Re: converting letters to numbers

Posted 10 November 2009 - 11:18 AM

Hi
```char letter_one='a';
char letter_two='b';
int sum = letter_one+letter_two; //you typed it wrong

cout<<sum;
```
this will print 97 which is sum of ASCII equivalents of characters a and b which are 97 and 98.

### #3 yoshiman

Reputation: 0
• Posts: 53
• Joined: 13-October 09

## Re: converting letters to numbers

Posted 10 November 2009 - 11:21 AM

I figured it would do that much, but is there a way to assign each letter of the alphabet an int number based on its position in the alphabet? Such as a=1 and b=2

### #4 Anarion

• The Persian Coder

Reputation: 387
• Posts: 1,663
• Joined: 16-May 09

## Re: converting letters to numbers

Posted 10 November 2009 - 11:34 AM

yes! that's what enums do:
```enum sample {
a=1, b, c //a is 1, b is 2, c is 3
};
sample s = b;
```

visit here for greater detail on their usage.

Edit: but you have to check for the character input yourself, and assign proper values in the enum variable.

This post has been edited by Anarion: 10 November 2009 - 11:40 AM

### #5 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: converting letters to numbers

Posted 10 November 2009 - 11:42 AM

so take a look at the ASCII table

'a' = 97
'b' = 98
sum = 97 + 98 = 195;

generally if you want to preform addition/subtraction over the alphabet what you do is something like this:

```char first_letter = 'g' - 'a'; // == 6
char second_letter = 'b' - 'a'; // == 1
char result = (first_letter + second_letter) % 26 + 'a';
```

by subtracting 'a' we convert from ASCII to the range 0 - 25 -- (which is mod 26) so then we can preform addition/subtraction mod 26, to convert back, we just need to add 'a'.

### #6 yoshiman

Reputation: 0
• Posts: 53
• Joined: 13-October 09

## Re: converting letters to numbers

Posted 10 November 2009 - 11:43 AM

Doing that would result in really messy code, I don't want to have to type out every letter corresponding them to their position in the alphabet.

this was a reply to Anarion

This post has been edited by yoshiman: 10 November 2009 - 11:44 AM

### #7 Anarion

• The Persian Coder

Reputation: 387
• Posts: 1,663
• Joined: 16-May 09

## Re: converting letters to numbers

Posted 10 November 2009 - 11:46 AM

Quote

this will print 97 which is sum of ASCII equivalents of characters a and b which are 97 and 98.

Ooh so sorry, I meant 195 I have become so careless recently, sorry guys

@yoshiman: yes that would be really messy!... another way can be: when you assign letter numbers to variables, decrease them by 96, this way you will get their values as a=1 b=2 and stuff

This post has been edited by Anarion: 10 November 2009 - 11:48 AM