By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

Help with Program

P: 1
I am trying to make a c++ program run as if you were text messaging on your cell phone 222=c 3=d 33=e and so on. I have no clue what the statement would be to set all the numbers to certain letters.. Someone please help!!
Sep 15 '06 #1
Share this Question
Share on Google+
1 Reply


risby
P: 30
I am trying to make a c++ program run as if you were text messaging on your cell phone 222=c 3=d 33=e and so on. I have no clue what the statement would be to set all the numbers to certain letters.. Someone please help!!
You could probably do something with a 26 element integer array whereby the first element represents the letter 'a' and is initialized with the value 2 to represent 1 press of the '2' key; the second element representing 'b' contains 22 and so on up to the 26th element representing 'z' and containing 9999.

Now if the input string to be encoded into txt contains 'dude' you take each letter in turn and subtract 'a' using the result to index into your array.

ascii value of 'd' is 100, ascii value of 'a' is 97, char data type in C is much the same as a one byte integer:

'd' - 'a' = 100 - 97 = 3, arrays are indexed from zero so 3 is fourth element and has been initialized with the value 3

'u' - 'a' = 117 - 97 = 20 and the twenty-first element will have been initialized with the value 88
Expand|Select|Wrap|Line Numbers
  1. cout >> input_string >> " is " >> endl;
  2. for (i=0; i<strlen(input_string); i++)
  3.     cout >> "\t" >> keys[input_string[i] - 'a'] >> endl;
  4.  
it will be more complicated than this to allow for upper and lower case, for symbols as well as letters and it does assume your computer uses ascii (it will unless you are using an IBM mainframe).
Sep 15 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.