I need to do the work below using c programming
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
i am really new to programming and c programming...can anyone please help me and show me example code...URgent...thank u
Translating alphabetic number
Started by changwl8888, Mar 17 2010 06:03 AM
12 replies to this topic
#1
Posted 17 March 2010 - 06:03 AM
|
|
|
#2
Posted 17 March 2010 - 07:21 AM
I would use a case statement.
#3
Posted 17 March 2010 - 07:25 AM
This appears to boil right down to two functions that convert alphabetical characters to their telephone numeric counterparts, you can simply ignore any input that is not a-z or A-Z. If your program needs to check if it's a valid phone number than the program would need some verification functions as well, but that shouldn't be too hard to write.
Anyway, I'd write the first function with type signature char getNumber(char letter), which just takes any arbitrary character and, if it's a letter, returns the according number. I'd do it with two strings, one with letters, and the other numbers, and just have a loop that compares them and if it finds the letter returns the according number. The second function should just take a character array of arbitrary length and return the converted string using the aforementioned function.
Writing a code example would kind of defeat the purpose of learning how to do it. If you're familiar with character arrays, if statements, and for loops you should be able to figure this out. Being a programmer involves not just writing code, it's about solving problems with that code, and this is just one of the many problems you'll inevitably need to solve. :)
Anyway, I'd write the first function with type signature char getNumber(char letter), which just takes any arbitrary character and, if it's a letter, returns the according number. I'd do it with two strings, one with letters, and the other numbers, and just have a loop that compares them and if it finds the letter returns the according number. The second function should just take a character array of arbitrary length and return the converted string using the aforementioned function.
Writing a code example would kind of defeat the purpose of learning how to do it. If you're familiar with character arrays, if statements, and for loops you should be able to figure this out. Being a programmer involves not just writing code, it's about solving problems with that code, and this is just one of the many problems you'll inevitably need to solve. :)
Wow I changed my sig!
#4
Posted 17 March 2010 - 08:18 PM
#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
Edited by ZekeDragon, 17 March 2010 - 09:41 PM.
Please use [code] tags when posting code.
#5
Posted 17 March 2010 - 09:11 PM
I would use
Wouldn't that be more efficient than a switch with 26 cases? It's certainly shorter, lol.
The max comparisons in the switch would be 26.
The max comparisons in the if..else if.. would be 18.
So if you were converting a ton of numbers it would save some calculations.
If I'm mistaken please let me know. It just seems like this is a better course of action since you have several consecutive characters that give you the same numerical value.
if(arrayChar[x] < 'D') //2 comparisons total
arrayInt[x] = 2;
else if(arrayChar[x] < 'G') //4 comparisons total
arrayInt[x] = 3;
else if(arrayChar[x] < 'J') //6 comparisons total
arrayInt[x] = 4;
else if(arrayChar[x] < 'M') //8 comparisons total
arrayInt[x] = 5;
else if(arrayChar[x] < 'P') //10 comparisons total
arrayInt[x] = 6;
else if(arrayChar[x] < 'T') //12 comparisons total
arrayInt[x] = 7;
else if(arrayChar[x] < 'W') //14 comparisons total
arrayInt[x] = 8;
else if(arrayChar[x] < '[') //16 comparisons total
arrayInt[x] = 9;
Wouldn't that be more efficient than a switch with 26 cases? It's certainly shorter, lol.
The max comparisons in the switch would be 26.
The max comparisons in the if..else if.. would be 18.
So if you were converting a ton of numbers it would save some calculations.
If I'm mistaken please let me know. It just seems like this is a better course of action since you have several consecutive characters that give you the same numerical value.
Edited by ZekeDragon, 17 March 2010 - 09:42 PM.
Please use [code] tags when posting code.
#6
Posted 17 March 2010 - 09:48 PM
changwl8888 said:
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
any tips? thanks alot
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Download the new operating system programming kit! (some assembly required)
#7
Posted 17 March 2010 - 10:49 PM
... O_o Really?
I'm gonna leave how to use this function to you. You could use a loop for strlen of the string, which is probably what I'd do, since you're most likely malloc()'ing your string sizes, unless your program is going to write over the array you'll have to store the result in a separate array, which will also need to be malloc()'d. Either way, I'm leaving that implementation for you, I'd just use a for loop over each character, apply that character in the function, and place the result in another array, then printf("%s\n") that string. No problem. ^_^
char getNumber(char letter) {
char *convert = (letter > 'Z') ? "abcdefghijklmnopqrstuvwxyz":
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; convert[i] != '\0' ; ++i)
if (convert[i] == letter) return "22233344455566677778889999"[i];
return letter;
}
The best thing you could do is to Do The Simplest Thing That Could Possibly Work. Generally speaking, I don't worry about optimization unless it becomes obvious that it is actually necessary, the computer can perform a lot of comparisons pretty fast, but if you ARE processing a great deal of numbers and this does become a bottleneck, here's a simple optimized version...char getNumber(char letter) {
char compare = letter;
if (compare >= 'Z') {
compare -= 32; // Assuming ASCII...
}
if (compare < 'A' || compare > 'Z') return letter;
for (int i = 0; ; ++i)
if (compare >= "WTPMJGDA"[i]) return "98765432"[i];
}
Max 11 comparisons and one subtraction.I'm gonna leave how to use this function to you. You could use a loop for strlen of the string, which is probably what I'd do, since you're most likely malloc()'ing your string sizes, unless your program is going to write over the array you'll have to store the result in a separate array, which will also need to be malloc()'d. Either way, I'm leaving that implementation for you, I'd just use a for loop over each character, apply that character in the function, and place the result in another array, then printf("%s\n") that string. No problem. ^_^
Wow I changed my sig!
#8
Posted 18 March 2010 - 03:09 PM
How about ...
char arrayCompare[26] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'}
if (arrayTest[x] > 57) //it is not a digit
arrayTest[x] = arrayCompare[arrayTest[x]-65];
2 comparisons. one subtraction.
Your code was cool btw, I have no idea what it does though :)
char arrayCompare[26] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'}
if (arrayTest[x] > 57) //it is not a digit
arrayTest[x] = arrayCompare[arrayTest[x]-65];
2 comparisons. one subtraction.
Your code was cool btw, I have no idea what it does though :)
#9
Posted 18 March 2010 - 03:59 PM
It's on. XD
And your code doesn't consider caps and non-caps.
And your code doesn't consider caps and non-caps.
char getNumber(char letter)
{
char get = letter;
if (get > 'Z') get -= 32; // Assuming ASCII
if (get < 'A' || get > 'Z') return letter;
return "22233344455566677778889999"[get - 'A']
}Max three comparisons, two subtractions. Lowercase included. :P
Wow I changed my sig!
#10
Posted 18 March 2010 - 04:21 PM
not fair i was counting if's as 2 comparisons lol. one for the > and one to check if the expression was true.
at most 2 comparisons, 1 subtractions:
at most 2 comparisons, 1 subtractions:
char arrayCompare[26] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'}//pass this to the function dont re declare it every time
void convert(char* arrayCompare, char* arrayTest, int x) // x is index num of test array (phone number)
{
if (arrayTest[x] > 57) //it is not a digit
{
if (arrayTest[x] > 'Z')
arrayTest[x] = arrayCompare[arraytest[x] -97]; // Assuming ASCII <--- yoink
else
arrayTest[x] = arrayCompare[arrayTest[x]-65];
}
return;
}
#11
Posted 18 March 2010 - 06:15 PM
//returns ASCII 0 for all non-letters
char getnum(unsigned char l) {
return "0000000000000000000000000000000000000000000000000000000000000000022233344455566677778889999000000222333444555666777788899990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"[(int)l];
}
At most zero comparisons and zero subtractions. You have both been 0wn3d.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Download the new operating system programming kit! (some assembly required)
#12
Posted 18 March 2010 - 08:19 PM
Microsoft: "You've got questions. We've got dancing paperclips


Sign In
Create Account

Back to top









