• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addition of 2 characters

 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Can you guide me on how to add 2 characters say a= '1' , b = '2' to yield a 3 (Not 99 ie 49 + 50; the ascii values)?

Thanks
Faisal
 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a constraint that this needs to be done without wrapper classes
 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that we need to traverse through all the ascii values, and lookup until we find a match ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have the character '1', which has a numerical value of 49, but you want to treat it as numerical value 1, and you have character '2', which has a numerical value of 50 but you want to treat it as a numerical value of 2?

So, you want the following mapping?



Yes? Look at the pattern above, and also consider the fact that you can do arithmetic on characters. Does that give you an idea of how to approach this?
 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the fact that I need to have a map.
I realise we can do arithmetic, but the results would be different.

char 'a' + 'b' = 99// not 3

Thanks
Faisal
 
Campbell Ritchie
Sheriff
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff said mapping, not a Map. There is a much simpler way to do such arithmetic, using chars only.
 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,

yes. I also meant mapping.

[There is a much simpler way to do such arithmetic, using chars only. ]
Could you give an example please ?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faisal syed wrote:Hi Ritchie,

yes. I also meant mapping.

[There is a much simpler way to do such arithmetic, using chars only. ]
Could you give an example please ?

If you had the value x but you actually wanted the value 0, what would you take away from x to get 0.
If you had the value (x + 1) but you actually wanted the value 1, what would you take away from (x + 1) to get 1.
If you had the value (x + 2) but you actually wanted the value 2, what would you take away from (x + 2) to get 2.

Answer those questions and then hopefully the answer to your problem will become clear.
 
Campbell Ritchie
Sheriff
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faisal syed wrote: . . . yes. I also meant mapping. . . .
But you said map, which is different from mapping.
Could you give an example please ?
Stuart B has already given you as much information as you really need.
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Faisal syed wrote: . . . yes. I also meant mapping. . . .
But you said map, which is different from mapping.
Could you give an example please ?
Stuart B has already given you as much information as you really need.


This may be a case where the OP should step back, take out a pencil and paper, and work it out first. Everyone here seems to easily envision what needs to be done, but I would guess that the reason for this, is that we have done it many many times before. Someone who has never done this should work it out on paper -- even though the maths behind it is ridiculously easy.

Henry

 
Campbell Ritchie
Sheriff
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean, Henry. Sorry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic