473,479 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12929
Niheel
2,460 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
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
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. {
  97. printf("answer: %c",phoneNumber[x]);
  98. }
  99.    }
  100.  
  101.  
  102.  
  103. }
  104.  
Mar 18 '10 #10
newb16
687 Contributor
Does it compile without errors (if you tried it though)?
Mar 18 '10 #11
changwl8888
11 New Member
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
changwl8888
11 New Member
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 Recognized Expert Top Contributor
You're calling length on the x'th item of the array, not the array itself.
Mar 18 '10 #14
newb16
687 Contributor
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
changwl8888
11 New Member
#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 Contributor
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 Recognized Expert Top Contributor
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,426 Recognized Expert Top Contributor
You need break statements for cases G-R.
Mar 18 '10 #19
jkmyoung
2,057 Recognized Expert Top Contributor
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 Contributor
Even then it won't work, because the whole switch is executed only once after the line is read.
Mar 18 '10 #21

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

Similar topics

8
5376
by: fo | last post by:
Does anyone know if there is a free code available to convert numbers to text and/or text to numbers? Thanks
7
1422
by: rodchar | last post by:
hey all, i have a number field that contains a phone number. some of my data has area codes and some don't. can someone show me a way to attach area codes to the ones that need it. i have...
4
5231
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
10
14279
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
2
3985
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
0
2764
by: Adele | last post by:
Please help, I'm new to SQL and I use a script to import data and when I tried importing data tonight it gave me "error converting data type nvarchar to float". I have no idea what this error...
7
4627
by: laredotornado | last post by:
Hi, Using php 4.4.4, I am looking to parse a US phone number string into 3 smaller strings -- area code, the first part of the phone number (3 digits) and the last part of the phone number (4...
5
3011
by: m22j | last post by:
Hello, Hopefully you guys can help me out. I'm in "intro to programming" right now and I am using visual basic express 2008. The assignment that I have due is called passwordVerification. I have...
8
5479
crystal2005
by: crystal2005 | last post by:
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:...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.