Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Translating alphabetic number

  1. #1
    changwl8888 is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Translating alphabetic number

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Translating alphabetic number

    I would use a case statement.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Translating alphabetic number

    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!

  5. #4
    changwl8888 is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Translating alphabetic number

    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
    Last edited by ZekeDragon; 03-17-2010 at 10:41 PM. Reason: Please use [code] tags when posting code.

  6. #5
    Osnarf's Avatar
    Osnarf is offline Learning Programmer
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Re: Translating alphabetic number

    I would use
    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;
    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.
    Last edited by ZekeDragon; 03-17-2010 at 10:42 PM. Reason: Please use [code] tags when posting code.

  7. #6
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Translating alphabetic number

    Quote Originally Posted by changwl8888 View Post
    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
    A for loop and strlen would probably work just fine.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  8. #7
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Translating alphabetic number

    ... O_o Really?

    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;
    }
    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 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("&#37;s\n") that string. No problem. ^_^
    Wow I changed my sig!

  9. #8
    Osnarf's Avatar
    Osnarf is offline Learning Programmer
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Re: Translating alphabetic number

    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

  10. #9
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Translating alphabetic number

    It's on. XD

    And your code doesn't consider caps and non-caps.
    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']
    }
    Max three comparisons, two subtractions. Lowercase included.
    Wow I changed my sig!

  11. #10
    Osnarf's Avatar
    Osnarf is offline Learning Programmer
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Re: Translating alphabetic number

    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;
    }

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Translating a Visual Basic program to Java
    By bloodchains in forum Java Help
    Replies: 4
    Last Post: 10-01-2011, 10:11 AM
  2. Translating C# 1 to C# 4
    By Tonchi in forum C# Programming
    Replies: 1
    Last Post: 03-26-2011, 10:52 PM
  3. Visual Basic Assighment Help. (Translating pseudocode to visual basic)
    By Adolf B in forum Visual Basic Programming
    Replies: 1
    Last Post: 03-18-2011, 09:16 PM
  4. Replies: 8
    Last Post: 03-17-2010, 07:06 PM
  5. Translating Services
    By Lop in forum Software Development Tools
    Replies: 2
    Last Post: 06-17-2006, 01:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts