Connecting Tech Pros Worldwide Forums | Help | Site Map

Translating alphabetic number in C

crystal2005's Avatar
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 43
#1: Mar 17 '09
Hi guys,

Just like the title, i'm looking for the example of C program that translates an alphabetic phone number into numeric. The idea as the following output

Enter phone number: 1800-TEST-100

result: 1800-8378-100


so basically 2=ABC, 3=DEF, 4=GHI, . . . , 9 = WXYZ

So, all the non-alphabetic (E.g. dash, punctuations) will leave unchanged.

Can anyone give the pseudo code of how to handle the string input of the above idea??

Thank you.

Newbie
 
Join Date: Mar 2009
Posts: 5
#2: Mar 17 '09

re: Translating alphabetic number in C


Actually, it seems to be an "light problem"...
All you need to do is write an "while" or a "for" loop.
The loop should go from 0 (zero) to the ALPH_PHONE_SIZE-1.
Probably you will read the phone from the user and store the information in a array char (or string).
Then, inside the loop you should access each position of this array/string and compute the information.
If you use a "while" loop you must keep a counter manually in order to access the array/string positions gradually. Otherwise, using a "for" loop you access the position with the loop control variable.
To know what character is the contend of a specific array/string position, you can implement a "switch:case" structure.
For example:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. // inside the loop
  3. switch (phone_array[POS]):
  4.    case 'a':
  5.    case 'b':
  6.    case 'c':
  7.       value = 2;
  8.       break;
  9.    case 'd':
  10.    case 'e:
  11.    case 'f':
  12.       value = 3;
  13.       break;
  14. ...
  15.    default:
  16.       break;
  17. ...
  18.  
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 385
#3: Mar 17 '09

re: Translating alphabetic number in C


If maintainability is not an issue, I'd rather use lookup array and index it by input letter ( converted to upper case beforehand):
res = lookup[letter-'A'];
where array is like
int lookup[] = {2,2,2,3,3, ... };
Familiar Sight
 
Join Date: Jan 2007
Posts: 191
#4: Mar 22 '09

re: Translating alphabetic number in C


The following comments explain the code amplified algorithm. The convert() function is certainly a bit clunky and could do with an oil change.
Expand|Select|Wrap|Line Numbers
  1. {char a[25]={0};                            
  2. char c=0;
  3. while(cin.get(c) )                        //get the alpha-numeric string
  4. { for(int i=0;i<25;i++)
  5.  a[i]=c;                                      //place each in array
  6.  if(c>=0 && c<=9)cout.put(c);       //traverse array and print integers
  7.  else if(c>='A' && c<='Z')convert(c);  //call func to convert text
  8.  else cout.put(c);}                        //print any integers which follow
  9. }
End input with Ctrl+Z.
Expand|Select|Wrap|Line Numbers
  1. char convert (char c)         //convert function - text to ints
  2. {
  3. switch (c)
  4. {case 65:cout<<"2";break; //A
  5.  case 66: cout<<"2";break; //B
  6.  case 67: cout<<"2";break; //C    
  7.  case 68: cout<<"3";break; //D
  8.  case 69: cout<<"3";break; //E       
  9.  case 70: cout<<"3";break; //F
  10. ............etc.......................... 
  11. case 87: cout<<"9";break; //W
  12.  case 88: cout<<"9";break; //X    
  13.  case 89: cout<<"9";break; //Y
  14.  case 90: cout<<"9";break; //Z       
  15.  default:cout<<" ";break; }                     
  16. }
/*
This is typical output.
1300-WIDER-32541-100^Z
1300-94337-32541-100

1800-THINNER-25416-100^Z
1800-8446637-25416-100^Z
Press any key to continue . .
*/
End program with another Ctrl+Z
Hope this helps.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Mar 22 '09

re: Translating alphabetic number in C


Quote:

Originally Posted by whodgson View Post

Expand|Select|Wrap|Line Numbers
  1. char convert (char c)         //convert function - text to ints
  2. {
  3. switch (c)
  4. {case 65:cout<<"2";break; //A
  5.  case 66: cout<<"2";break; //B
  6.  case 67: cout<<"2";break; //C    
  7.  case 68: cout<<"3";break; //D
  8.  case 69: cout<<"3";break; //E       
  9.  case 70: cout<<"3";break; //F
  10. ............etc.......................... 
  11. case 87: cout<<"9";break; //W
  12.  case 88: cout<<"9";break; //X    
  13.  case 89: cout<<"9";break; //Y
  14.  case 90: cout<<"9";break; //Z       
  15.  default:cout<<" ";break; }                     
  16. }

Please don't try to outsmart anything and don't use those absolute values; your code fails horribly on non-ASCII encoding machines (EBCDIC is still around and alive and kicking). Use symbolic constants instead: 'A', 'B', 'C' etc. Your compiler knows what to do with them.

kind regards,

Jos
Familiar Sight
 
Join Date: Jan 2007
Posts: 191
#6: Mar 23 '09

re: Translating alphabetic number in C


The convert() function in thread #4 was badly written and wrong.
It should have been expressed as follows:
char convert (char c)
{
switch (c)
{case 'A':cout<<"2";break;
case 'B': cout<<"2";break;
case 'C': cout<<"2";break;
case 'D': cout<<"3";break;
case 'E': cout<<"3";break;
case 'F': cout<<"3";break;
--------------etc-------------------
case 'S': cout<<"7";break;
case 'T': cout<<"8";break;
case 'U': cout<<"8";break;
case 'V': cout<<"8";break;
case 'W': cout<<"9";break;
case 'X': cout<<"9";break;
case 'Y': cout<<"9";break;
case 'Z': cout<<"9";break;
default:cout<<" ";break; }
}
Thanks JosAH.....afraid am a VSL
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Mar 23 '09

re: Translating alphabetic number in C


Why not use the short solution given in reply #3? Something like this:

Expand|Select|Wrap|Line Numbers
  1. char* d= "222333444 ... 77778889999";
  2. if (c >= 'A' && c <= 'Z') cout << d[c-'A'];
  3.  
kind regards,

Jos
crystal2005's Avatar
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 43
#8: Mar 23 '09

re: Translating alphabetic number in C


Quote:

Originally Posted by whodgson View Post

The convert() function in thread #4 was badly written and wrong.
It should have been expressed as follows:
char convert (char c)
{
switch (c)
{case 'A':cout<<"2";break;
case 'B': cout<<"2";break;
case 'C': cout<<"2";break;
case 'D': cout<<"3";break;
case 'E': cout<<"3";break;
case 'F': cout<<"3";break;
--------------etc-------------------
case 'S': cout<<"7";break;
case 'T': cout<<"8";break;
case 'U': cout<<"8";break;
case 'V': cout<<"8";break;
case 'W': cout<<"9";break;
case 'X': cout<<"9";break;
case 'Y': cout<<"9";break;
case 'Z': cout<<"9";break;
default:cout<<" ";break; }
}
Thanks JosAH.....afraid am a VSL


Ahh... this works fine. Thanks a lot guys :)

But my program has different implementation. I used for loop to traverse all the character inside the char array and put the array into the switch case.
Familiar Sight
 
Join Date: Jan 2007
Posts: 191
#9: Mar 25 '09

re: Translating alphabetic number in C


I couldn`t see how Newb16`s lookup worked and thought that it was a germ of an idea that had to be developed. Now that you`ve repeated it, think I can see
(vaguely) its meaning....... so will have a play with it. Thanks to you both.
Reply

Tags
alphabetic, c programming, string


Similar C / C++ bytes