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
I would use a case statement.
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.![]()
Wow I changed my sig!
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?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; }
any tips? thanks alot
Last edited by ZekeDragon; 03-17-2010 at 10:41 PM. Reason: Please use [code] tags when posting code.
I would use
Wouldn't that be more efficient than a switch with 26 cases? It's certainly shorter, lol.Code: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;
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.
Last edited by ZekeDragon; 03-17-2010 at 10:42 PM. Reason: Please use [code] tags when posting code.
... O_o Really?
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...Code: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; }
Max 11 comparisons and one subtraction.Code: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]; }
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!
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![]()
It's on. XD
And your code doesn't consider caps and non-caps.
Max three comparisons, two subtractions. Lowercase included.Code: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'] }![]()
Wow I changed my sig!
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:
Code: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; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks