Program to find combination of letters associated with phone

Click For Summary
SUMMARY

The discussion revolves around creating a program that generates all possible letter combinations from a phone keypad input, specifically for the input number 23, which corresponds to the letters AD, AE, AF, BD, BE, BF, CD, CE, and CF. The original poster struggles with storing user input dynamically and generating combinations without using arrays, as they are still learning programming fundamentals. A suggestion is made to utilize recursion for generating combinations, which is more efficient than the initial approach involving bitwise operations and nested loops.

PREREQUISITES
  • Understanding of basic programming concepts in C
  • Familiarity with recursion techniques
  • Knowledge of character arrays and string manipulation in C
  • Basic understanding of phone keypad mappings
NEXT STEPS
  • Learn about recursion in C programming
  • Explore character arrays and string functions in the C standard library
  • Research how to implement backtracking algorithms for combination generation
  • Study dynamic memory allocation in C for handling variable input sizes
USEFUL FOR

Beginner programmers, computer science students, and anyone interested in algorithm design for generating combinations based on user input.

toforfiltum
Messages
341
Reaction score
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
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.
 
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.
 
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
 }
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
47
Views
5K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
18
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K