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
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.
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.
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: - switch (toupper(s[i])) {
-
case 'A':
-
case 'B':
-
case 'C':
-
s[i] = '2';
-
break;
-
...
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
can u show me some example code?in more details pls..
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."
-
#define SIZE 30
-
-
-
void translateNumber(int * optionStats, char * phoneNumber)
-
{
-
char 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;
-
}
-
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
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
is it correct way to print?
please correct me if i m wrong...thanks...
Does it compile without errors (if you tried it though)?
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
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
jkmyoung 2,057
Recognized Expert Top Contributor
You're calling length on the x'th item of the array, not the array itself.
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.
#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
How many edit-compile-(optionally run) iterations had you done? If less than 30, you may be trying not hard enough.
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.
donbock 2,426
Recognized Expert Top Contributor
You need break statements for cases G-R.
jkmyoung 2,057
Recognized Expert Top Contributor
That's true! Another thing that could be done is combining cases: - case 'T':
-
case 'U':
-
case 'V':
-
phoneNumber[x]="8";
-
break;
-
-
case 'X':
-
case 'Y':
-
case 'Z':
-
phoneNumber[x]="9";
-
break;
Even then it won't work, because the whole switch is executed only once after the line is read.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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:...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |