Program to find combination of letters associated with phone

In summary, the conversation discusses a problem of generating all possible letter combinations associated with a given number on a phone keypad. The speaker suggests a complicated solution involving bitwise operations and counting the number of unique numbers entered by the user. They mention being stuck on how to store the different numbers and generating the combinations, and express their desire for a simpler solution. They also mention being a beginner in programming and not having learned about arrays yet.
  • #1
toforfiltum
341
4
So here is the question:

On a phone keypad, many of the numbers have letters associated with them. For instance, the letters A, B, and C are associated with the number 2. Write a program that accepts a number as input and prints all of the possible letter combinations associated with that number. For example, if the input is n=23, the possible letter combinations are AD, AE, AF, BD, BE, BF, CD, CE, and CF.

I don't know how to solve this problem, but here is my idea, and the accompanying program. My idea is that since there are just 8 unique numbers, I would go through each number typed by the user, and if it is new, do a form of bitwise operation to record it. Therefore, if the user were to repeat that number again, there is some sort of memory.

Then after that, I would write a program to count the number of 1s and therefore, the different unique numbers entered by user.

Here is where I get stuck. I just can't seem to figure out how to 'store' the different numbers, because I don't know beforehand while writing the program how many the user would enter. And I don't really know how to generate all the combinations though I think a nested for loop could do it.

Anyway, I thought that my idea is way too complicated and should be simpler, since I'm in a beginner programming course. I have not learned arrays yet too. So any ideas on how to tackle this problem is much appreciated.

C:
#include <stdio.h>

 int main ()
   {
       int input, i, j, k = 0, count_1 = 0, count_2 = 0, integer, check, monitor = 0;
    char alphabets;

    printf("Enter number as input: ");
    scanf("%d", &input);

if ( input < 50 || input > 57 )
{
   printf("Enter number as input: ");
   scanf("%d", &input);
}
integer = input;
while ( integer > 0 )
{
   check = input % 10;
   input = integer / 10;

   switch ( check )
   {
       case 2: monitor | ( 2 * 1 );
       break;

       case 3 : monitor | ( 2 * 10 );
       break;

       case 4 : monitor | ( 2 * 100 );
       break;

       case 5 : monitor | ( 2 * 1000 );
       break;

       case 6 : monitor | ( 2 * 10000 );
       break;

       case 7 : monitor | ( 2 * 100000 );
       break;

       case 8 : monitor | ( 2 * 1000000 );
       break;

       case 9 : monitor | ( 2 * 10000000 ) ;
       break;
   }

}

for ( i = 0; i < 8; i++ )
{
   if ( i == 0 )
   {
       power = 1;
   }
   else
   {
       for ( j = 1; j <= i; j++ )
       {
           power *= 10;
       }
   }
}

while ( k < 7 )
{
 
Technology news on Phys.org
  • #2
toforfiltum said:
So here is the question:

On a phone keypad, many of the numbers have letters associated with them. For instance, the letters A, B, and C are associated with the number 2. Write a program that accepts a number as input and prints all of the possible letter combinations associated with that number. For example, if the input is n=23, the possible letter combinations are AD, AE, AF, BD, BE, BF, CD, CE, and CF.

I don't know how to solve this problem, but here is my idea, and the accompanying program. My idea is that since there are just 8 unique numbers, I would go through each number typed by the user, and if it is new, do a form of bitwise operation to record it. Therefore, if the user were to repeat that number again, there is some sort of memory.

Then after that, I would write a program to count the number of 1s and therefore, the different unique numbers entered by user.

Here is where I get stuck. I just can't seem to figure out how to 'store' the different numbers, because I don't know beforehand while writing the program how many the user would enter. And I don't really know how to generate all the combinations though I think a nested for loop could do it.

Anyway, I thought that my idea is way too complicated and should be simpler, since I'm in a beginner programming course. I have not learned arrays yet too. So any ideas on how to tackle this problem is much appreciated.

C:
#include <stdio.h>

 int main ()
   {
       int input, i, j, k = 0, count_1 = 0, count_2 = 0, integer, check, monitor = 0;
    char alphabets;

    printf("Enter number as input: ");
    scanf("%d", &input);

if ( input < 50 || input > 57 )
{
   printf("Enter number as input: ");
   scanf("%d", &input);
}
integer = input;
while ( integer > 0 )
{
   check = input % 10;
   input = integer / 10;

   switch ( check )
   {
       case 2: monitor | ( 2 * 1 );
       break;

       case 3 : monitor | ( 2 * 10 );
       break;

       case 4 : monitor | ( 2 * 100 );
       break;

       case 5 : monitor | ( 2 * 1000 );
       break;

       case 6 : monitor | ( 2 * 10000 );
       break;

       case 7 : monitor | ( 2 * 100000 );
       break;

       case 8 : monitor | ( 2 * 1000000 );
       break;

       case 9 : monitor | ( 2 * 10000000 ) ;
       break;
   }

}

for ( i = 0; i < 8; i++ )
{
   if ( i == 0 )
   {
       power = 1;
   }
   else
   {
       for ( j = 1; j <= i; j++ )
       {
           power *= 10;
       }
   }
}

while ( k < 7 )
{
I don't see any way to do this without using strings of three characters, and being able to pick the individual characters out of them. You wouldn't necessarily have to use array indexing to get the characters, as there are functions in the C standard library header (string.h) that could be used. Unfortunately, using them is probably more difficult than learning how to access elements of an array.

A lot of your code doesn't do anything useful.
C:
switch ( check )
   {
       case 2: monitor | ( 2 * 1 );
       break;
       // <snip>
The expression monitor | (2 * 1) is evaluated, but is not stored anywhere, and not printed, nor is anything done with it. Including this expression has exactly the same effect as not including it.
 
  • #3
Mark44 said:
I don't see any way to do this without using strings of three characters, and being able to pick the individual characters out of them. You wouldn't necessarily have to use array indexing to get the characters, as there are functions in the C standard library header (string.h) that could be used. Unfortunately, using them is probably more difficult than learning how to access elements of an array.

A lot of your code doesn't do anything useful.
C:
switch ( check )
   {
       case 2: monitor | ( 2 * 1 );
       break;
       // <snip>
The expression monitor | (2 * 1) is evaluated, but is not stored anywhere, and not printed, nor is anything done with it. Including this expression has exactly the same effect as not including it.
That's precisely the problem I had with my idea, I could not store those expressions because while writing the program, I do not know beforehand how many unique numbers the user would enter, thus I can't create a variable to store it. Do I make sense? I hope I do.
 
  • #4
You are generating output that is exponentially larger than the input. That's a hint that the task is best handled using recursion.
Also, if you want the output in the order indicated, you're in some trouble since the %10 naturally leads to AD, BD, CD, AE, BE, CE ordering.
You could try something like
C:
  ...
  int input;
  char buffer[13]; //13 should be enough for a 32-bit int
  //...input number into "input"
  sprintf(buffer, "%d", input);
  printRest(buffer, 0);
 } //end of program

void printRest(char buffer[13], int position)
 {
  //on input ("23",0) call itself with ("A3",1) then with ("B3",1)
    //then with ("C3",1) then restore the buffer to "23"
  //on input ("A3",1) call itself with ("AD",2) and so on
  //on input ("AD",2) print the buffer
 }
 

1. What is a program to find combination of letters associated with phone?

A program to find combination of letters associated with phone is a computer program designed to help users convert a phone number containing digits into a corresponding combination of letters. This is commonly used for creating memorable and personalized phone numbers, such as vanity numbers.

2. How does a program to find combination of letters associated with phone work?

A program to find combination of letters associated with phone typically uses a combination of algorithms and lookup tables to convert each digit in a phone number into its corresponding letter. The program then combines these letters to form a memorable word or phrase.

3. What are the benefits of using a program to find combination of letters associated with phone?

The main benefit of using a program to find combination of letters associated with phone is the ability to create personalized and memorable phone numbers. This can be especially useful for businesses or individuals who want to stand out and make their phone number more memorable to potential customers or clients.

4. Can a program to find combination of letters associated with phone be used for any type of phone number?

Yes, a program to find combination of letters associated with phone can be used for any type of phone number, including landline, mobile, and toll-free numbers. However, some phone numbers may not have enough digits to create a meaningful combination of letters.

5. Is it possible to create a phone number with a specific word using a program to find combination of letters associated with phone?

Yes, it is possible to create a phone number with a specific word using a program to find combination of letters associated with phone. Users can input the desired word and the program will generate a list of possible phone numbers that contain that word as a combination of letters.

Similar threads

  • Programming and Computer Science
Replies
4
Views
737
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
622
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
2
Replies
55
Views
4K
Back
Top