Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Telephone Words

Name: Anonymous 2011-05-22 9:12

People often give others their telephone number as a word representing the seven-digit number. For example, if my telephone number were 866-2665,1 could tell people my number is "TOOCOOL," instead of the hard-to-remember seven-digit number. Note that many other possibilities (most of which an) nonsensical) can rep-resent 866-2665. You can see how letters correspond to numbers on a telephone key-pad.

Write a routine that takes a seven-digit telephone number and prints out all of the passible "words" or combinations of letters that can represent the given number. Because the 0 and 1 keys have no letters on them, you should change only the digits 2-9 to letters. You'll be passed an array of seven integers, with each element being one digit in the number. You may assume that only valid phone numbers will be passed to your routine. You can use the helper routine char getCharKeyl zr.: zelephoneKey, int place ) which takes a telephone key (0-9) and a place of either 1, 2,3 and returns the charac-ter corresponding to the letter in that position on the specified key. For example, GetCharKey(3, 2) will return 'E' because the telephone key 3 has the letters "D [F" on it and 'E' is the second letter.

Name: Anonymous 2011-05-23 1:56


#include <stdio.h>

char GetCharKey (int num, int letter)
{
  char *letters[] =
  {
    "00", "01",
    "0ABC", "0DEF", "0GHI", "0JKL",
    "0MNO", "0PQRS", "0TUV", "0WXYZ"
  };
  return letters[num][letter];
   
}

int sizes[] =
{
  1, 1,
  3, 3, 3, 3,
  3, 4, 3, 4
};

int digits[7];
int st[8];

void bt (int k)
{
  int i;
  st[k] = 0;
  while (st[k] < sizes[digits[k-1]])
  {
    st[k]++;
    if (k == 7)
    {
      for (i = 1; i <= 7; i++)
        printf ("%c", GetCharKey (digits[i-1], st[i]));
      puts ("");
    }
    else
      bt (k+1);
  }
}

int main (void)
{
  unsigned int number;
  int i = 0, temp;

  scanf ("%u", &number);

  //generates number array
  while (number)
  {
    digits[i++] = number % 10;
    number /= 10;
  }

  //reverses number array so the digits are in proper order
  for (i = 0; i < 7/2; i++)
  {
    temp = digits[i];
    digits[i] = digits[7-i-1];
    digits[7-i-1] = temp;
  }

  bt (1);

  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List