473,976 Members | 36,977 Online

# Converting alphabetic phone number to numeric

11 New Member
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 13012
Niheel
2,456 Recognized Expert Moderator Top Contributor
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 Contributor
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,426 Recognized Expert Top Contributor
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
changwl8888
11 New Member
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
changwl8888
11 New Member
can u show me some example code?in more details pls..
Mar 17 '10 #6
donbock
2,426 Recognized Expert Top Contributor
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
changwl8888
11 New Member
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 Contributor
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
changwl8888
11 New Member
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. {
98. }
99.    }
100.
101.
102.
103. }
104.
Mar 18 '10 #10