472,096 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Converting alphabetic phone number to numeric

How do i convert an alphabetic phone number to a numeric phone number?

Translating alphabetic number:

Enter phone number:1-800-COL-LECT

1-800-265-5328
Mar 17 '10 #1
20 12682
Niheel
2,452 Expert Mod 2GB
Simplest way is create an array and assign each letter it's corresponding number. Then loop through the phone number input and replace the letter with number.
Mar 17 '10 #2
whodgson
542 512MB
Uhmm....I think:
if string s=1800-COL-LECT
If(s[i]=='-') write nothing
else ii(s[i]>==0 || s[i]<==9)write s[i]
if(s[i]=='A'||s[i]=='B'||s[i]=='C')write 2
etc for DEF,GHI,JKL,MNO,PQRS,TUV and WXYZ.
Mar 17 '10 #3
donbock
2,425 Expert 2GB
I suggest using <ctype.h> classification functions isdigit() and isalpha(). You probably want to treat 'a' the same as 'A'. Instead of the logical expression in the if statement, I suggest using a switch statement -- it will be easier to read:
Expand|Select|Wrap|Line Numbers
  1. switch (toupper(s[i])) {
  2.    case 'A':
  3.    case 'B':
  4.    case 'C':
  5.       s[i] = '2';
  6.       break;
  7.    ...
Mar 17 '10 #4
hi..sorry i don't really understand how to convert and store and show all number back...can u please show more details of code...i am really new to c programming
Mar 17 '10 #5
can u show me some example code?in more details pls..
Mar 17 '10 #6
donbock
2,425 Expert 2GB
Refer to the posting guidelines.
We'll answer questions like "why did I get this error?"; we won't answer questions like "I don't know how to start, please send me the code."
Mar 17 '10 #7
Expand|Select|Wrap|Line Numbers
  1. #define SIZE 30
  2.  
  3.  
  4. void translateNumber(int * optionStats, char * phoneNumber)
  5. {
  6.     char phoneNumber[SIZE];
  7.     int x=0;
  8.     int length;
  9.  
  10. printf("Translating alphatic number:\n");
  11. printf("Enter Phone Number:\n");
  12.    while (fgets(phoneNumber, SIZE, stdin) != NULL)  /* fetch up to 30 chars */
  13.    {
  14.          switch (toupper(phoneNumber[x]))
  15.             {
  16.                     case 'A':
  17.                     phonenumber[x]="2";
  18.                     break;
  19.                     case 'B':
  20.                     phonenumber[x]="2";
  21.  
  22.                     break;
  23.                     case 'C':
  24.                     phonenumber[x]="2";
  25.  
  26.                     break;
  27.                     case 'D':
  28.                     phonenumber[x]="3";
  29.  
  30.                     break;
  31.                     case 'E':
  32.                      phonenumber[x]="3";
  33.                     break;
  34.                     case 'F':
  35.                      phonenumber[x]="3";
  36.                     break;
  37.                     case 'G':
  38.                      phonenumber[x]="4";
  39.                     case 'H':
  40.                     phonenumber[x]="4";
  41.  
  42.                     case 'I':
  43.                     phonenumber[x]="4";
  44.                     case 'J':
  45.                      phonenumber[x]="5";
  46.                     case 'K':
  47.                     phonenumber[x]="5";
  48.  
  49.                     case 'L':
  50.                     phonenumber[x]="5";
  51.  
  52.                     case 'M':
  53.                     phonenumber[x]="6";
  54.  
  55.                     case 'N':
  56.                     phonenumber[x]="6";
  57.  
  58.                     case 'O':
  59.                     phonenumber[x]="6";
  60.  
  61.                     case 'P':
  62.                     phonenumber[x]="7";
  63.  
  64.                     case 'Q':
  65.                     phonenumber[x]="7";
  66.  
  67.                     case 'R':
  68.                     phonenumber[x]="7";
  69.  
  70.                     case 'S':
  71.                     phonenumber[x]="7";
  72.                     break;
  73.                     case 'T':
  74.                     phonenumber[x]="8";
  75.                     break;
  76.                     case 'U':
  77.                     phonenumber[x]="8";
  78.                     break;
  79.                     case 'V':
  80.                     phonenumber[x]="8";
  81.                     break;
  82.                     case 'W':
  83.                     phonenumber[x]="9";
  84.                     break;
  85.                     case 'X':
  86.                     phonenumber[x]="9";
  87.                     break;
  88.                     case 'Y':
  89.                     phonenumber[x]="9";
  90.                     break;
  91.                     case 'Z':
  92.                     phonenumber[x]="9";
  93.                     break;
  94.                     }
  95.  
so this is some code i have done...but i duno how to make it...store and display back...is it using for loop n strlen?
any tips? thanks alot
Mar 18 '10 #8
whodgson
542 512MB
Read the threads above. .
Essentially you have converted the alphanumeric number to numeric. Now print it
using a for loop.
To improve your code you can group cases 'A','B'and'C' as suggested in thread #4
Mar 18 '10 #9
is it correct way to print?
please correct me if i m wrong...thanks...

Expand|Select|Wrap|Line Numbers
  1. #define SIZE 30
  2.  
  3. void translateNumber(int * optionStats, char * phoneNumber)
  4. {
  5.     phoneNumber[SIZE];
  6.     int x=0;
  7.     int length;
  8.  
  9. printf("Translating alphatic number:\n");
  10. printf("Enter Phone Number:\n");
  11.    while (fgets(phoneNumber, SIZE, stdin) != NULL)  /* fetch up to 30 chars */
  12.    {
  13.          switch (toupper(phoneNumber[x]))
  14.             {
  15.                     case 'A':
  16.                     phoneNumber[x]="2";
  17.                     break;
  18.                     case 'B':
  19.                     phoneNumber[x]="2";
  20.  
  21.                     break;
  22.                     case 'C':
  23.                     phoneNumber[x]="2";
  24.  
  25.                     break;
  26.                     case 'D':
  27.                     phoneNumber[x]="3";
  28.  
  29.                     break;
  30.                     case 'E':
  31.                      phoneNumber[x]="3";
  32.                     break;
  33.                     case 'F':
  34.                      phoneNumber[x]="3";
  35.                     break;
  36.                     case 'G':
  37.                      phoneNumber[x]="4";
  38.                     case 'H':
  39.                     phoneNumber[x]="4";
  40.  
  41.                     case 'I':
  42.                     phoneNumber[x]="4";
  43.                     case 'J':
  44.                      phoneNumber[x]="5";
  45.                     case 'K':
  46.                     phoneNumber[x]="5";
  47.  
  48.                     case 'L':
  49.                     phoneNumber[x]="5";
  50.  
  51.                     case 'M':
  52.                     phoneNumber[x]="6";
  53.  
  54.                     case 'N':
  55.                     phoneNumber[x]="6";
  56.  
  57.                     case 'O':
  58.                     phoneNumber[x]="6";
  59.  
  60.                     case 'P':
  61.                     phoneNumber[x]="7";
  62.  
  63.                     case 'Q':
  64.                     phoneNumber[x]="7";
  65.  
  66.                     case 'R':
  67.                     phoneNumber[x]="7";
  68.  
  69.                     case 'S':
  70.                     phoneNumber[x]="7";
  71.                     break;
  72.                     case 'T':
  73.                     phoneNumber[x]="8";
  74.                     break;
  75.                     case 'U':
  76.                     phoneNumber[x]="8";
  77.                     break;
  78.                     case 'V':
  79.                     phoneNumber[x]="8";
  80.                     break;
  81.                     case 'W':
  82.                     phoneNumber[x]="9";
  83.                     break;
  84.                     case 'X':
  85.                     phoneNumber[x]="9";
  86.                     break;
  87.                     case 'Y':
  88.                     phoneNumber[x]="9";
  89.                     break;
  90.                     case 'Z':
  91.                     phoneNumber[x]="9";
  92.                     break;
  93.                     }
  94.  
  95. for (phoneNumber[x],phoneNumber[x].length;phoneNumber++)
  96. {
  97. printf("answer: %c",phoneNumber[x]);
  98. }
  99.    }
  100.  
  101.  
  102.  
  103. }
  104.  
Mar 18 '10 #10
newb16
687 512MB
Does it compile without errors (if you tried it though)?
Mar 18 '10 #11
it is error on this line
for (phoneNumber[x];phoneNumber[x].length;phoneNumber++)

request for member "length" in something not a structure or union...


help please
Mar 18 '10 #12
it is error on this line
for (phoneNumber[x];phoneNumber[x].length;phoneNumber++)

request for member "length" in something not a structure or union...

when compile error....
help please
Mar 18 '10 #13
jkmyoung
2,057 Expert 2GB
You're calling length on the x'th item of the array, not the array itself.
Mar 18 '10 #14
newb16
687 512MB
phonenumber[i] is char, you can't access char using dot as if it was a structure with a member named length. And array has no .length as well. Also, for() loop contains three expressions separated by semicolon - initializer, condition and increment, maybe optional but semicolons are not.
Mar 18 '10 #15
#include "assign1.h"

/* This source file contains important functions to be developed and
* used by various menu options, as indicated. Note that every
* function has as its first parameter the optionsStats array, which
* will be appropriately updated for later reporting of menu option 6.
* You may ignore this parameter and its relevance to each function
* until you develop the sessionSummary() function.
*/
#define SIZE 30

/* See requirement #2 "Translating alphabetic number" of the
* assignment specs.
*/
void translateNumber(int * optionStats, char * phoneNumber)
{
phoneNumber[SIZE];
int x=0;
int length;

printf("Translating alphatic number:\n");
printf("Enter Phone Number:\n");
while (fgets(phoneNumber, SIZE, stdin) != NULL) /* fetch up to 30 chars */
{
switch (toupper(phoneNumber[x]))
{
case 'A':
phoneNumber[x]="2";
break;
case 'B':
phoneNumber[x]="2";

break;
case 'C':
phoneNumber[x]="2";

break;
case 'D':
phoneNumber[x]="3";

break;
case 'E':
phoneNumber[x]="3";
break;
case 'F':
phoneNumber[x]="3";
break;
case 'G':
phoneNumber[x]="4";
case 'H':
phoneNumber[x]="4";

case 'I':
phoneNumber[x]="4";
case 'J':
phoneNumber[x]="5";
case 'K':
phoneNumber[x]="5";

case 'L':
phoneNumber[x]="5";

case 'M':
phoneNumber[x]="6";

case 'N':
phoneNumber[x]="6";

case 'O':
phoneNumber[x]="6";

case 'P':
phoneNumber[x]="7";

case 'Q':
phoneNumber[x]="7";

case 'R':
phoneNumber[x]="7";

case 'S':
phoneNumber[x]="7";
break;
case 'T':
phoneNumber[x]="8";
break;
case 'U':
phoneNumber[x]="8";
break;
case 'V':
phoneNumber[x]="8";
break;
case 'W':
phoneNumber[x]="9";
break;
case 'X':
phoneNumber[x]="9";
break;
case 'Y':
phoneNumber[x]="9";
break;
case 'Z':
phoneNumber[x]="9";
break;
}



}
for (x=0;phoneNumber[x];x++)
{
printf("answer: %c",phoneNumber[x]);
}


optionStats[0]++;
}


this is code i have done...but i cant display result converted...helpplease
Mar 18 '10 #16
newb16
687 512MB
How many edit-compile-(optionally run) iterations had you done? If less than 30, you may be trying not hard enough.
Mar 18 '10 #17
jkmyoung
2,057 Expert 2GB
for (x=0;phoneNumber[x];x++)

At what condition do you want to stop?

Get a faster response to your questions by:
1. Using code tags!
[ code ] some code [ / code]
(remove spaces)
2. Posting the section of your code where you're having the problem, not the entire code.
Mar 18 '10 #18
donbock
2,425 Expert 2GB
You need break statements for cases G-R.
Mar 18 '10 #19
jkmyoung
2,057 Expert 2GB
That's true! Another thing that could be done is combining cases:
Expand|Select|Wrap|Line Numbers
  1. case 'T':
  2. case 'U':
  3. case 'V':
  4. phoneNumber[x]="8";
  5. break;
  6.  
  7. case 'X':
  8. case 'Y':
  9. case 'Z':
  10. phoneNumber[x]="9";
  11. break;
Mar 18 '10 #20
newb16
687 512MB
Even then it won't work, because the whole switch is executed only once after the line is read.
Mar 18 '10 #21

Post your reply

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

Similar topics

8 posts views Thread by fo | last post: by
7 posts views Thread by rodchar | last post: by
4 posts views Thread by Earl | last post: by
10 posts views Thread by JackM | last post: by
7 posts views Thread by laredotornado | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.