converting letters to numbers

just a simple question

Page 1 of 1

6 Replies - 1495 Views - Last Post: 10 November 2009 - 11:46 AM Rate Topic: -----

#1 yoshiman  Icon User is offline

  • D.I.C Head

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

converting letters to numbers

Post icon  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  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 yoshiman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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'.
Was This Post Helpful? 1
  • +
  • -

#6 yoshiman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • 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 :crazy: 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1