Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Program to find combination of letters associated with phone

  1. Mar 1, 2017 #1
    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.

    Code (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 )
    {
     
  2. jcsd
  3. Mar 1, 2017 #2

    Mark44

    Staff: Mentor

    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.
    Code (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.
     
  4. Mar 1, 2017 #3
    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.
     
  5. Mar 1, 2017 #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
    Code (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
     }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Program to find combination of letters associated with phone
  1. Associative MEmory (Replies: 9)

Loading...