# converting telephone numbers to letters with all combinations

Page 1 of 1

## 6 Replies - 23878 Views - Last Post: 21 April 2009 - 01:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=50805&amp;s=6f0c15244d5981354e98e22362cb582f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 veena123

Reputation: 0
• Posts: 4
• Joined: 02-May 08

# converting telephone numbers to letters with all combinations

Posted 02 May 2008 - 01:08 AM

Hi,

Im trying to write a program which takes telephone number as input and shud didplay all combinations of letters.

for eg:if telephone number is : 234
the output shud be : adg,aeh,afi,aeg,aei.......all the combinations

how to proceed

*Edited surely does not desserved the "advanced" post tag

This post has been edited by pbl: 21 April 2009 - 05:30 PM

Is This A Good Question/Topic? 0

## Replies To: converting telephone numbers to letters with all combinations

### #2 .Maleficus.

Reputation: 2
• Posts: 129
• Joined: 07-March 08

## Re: converting telephone numbers to letters with all combinations

Posted 02 May 2008 - 03:42 AM

One way to do it would be to take the input from the user in a string, and then break each letter off into a char for an array. Then, create a loop to check which letter it is, and then nest a loop for the corresponding letter printing.

### #3 veena123

Reputation: 0
• Posts: 4
• Joined: 02-May 08

## Re: converting telephone numbers to letters with all combinations

Posted 02 May 2008 - 12:10 PM

.Maleficus., on 2 May, 2008 - 03:42 AM, said:

One way to do it would be to take the input from the user in a string, and then break each letter off into a char for an array. Then, create a loop to check which letter it is, and then nest a loop for the corresponding letter printing.

Hi

im keeping the numbers in the array,but unable to loop them

any help

int tel[] = new int[10];
char a2[] = {'a','b','c'};
char a3[] = {'d','e','f'};
char a4[] = {'g','h','i'};
---------- so on upto 9
int i = 0,t = 0;
Scanner in = new Scanner(System.in);
System.out.println("enter the telephone number");
for(i=0;i<10;i++)
{
tel[i] = Integer.parseInt(in.nextLine());
}
How to check the number and loop to get letters

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: converting telephone numbers to letters with all combinations

Posted 02 May 2008 - 02:04 PM

im keeping the numbers in the array,but unable to loop them

any help

```		// a [][] array with all the digit
char a[][] = {{'0'},	// 0 is 0 no choice
{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
---------- so on upto 9
{'w','x','y'}
};

Scanner in = new Scanner(System.in);
System.out.println("enter the telephone number");
// get phone on a String
String str = in.next();
// transalte into a char[]
char[] digit = str.toCharArray();
// scan all the digit
for(int i = 0; i < digit.lenght; i++ } {
// don't do the "-" if user put one
if(digit[i] == '-')
continue;
// translate digit into an int
int x = digit[i] - '0';
// now you can play with the
// a[x].length possible digit
for(int j = 0; j < a[x].length; j++) {
// a[x][j] will contain 1 by 1 all your possibility
}
}

```

### #5 veena123

Reputation: 0
• Posts: 4
• Joined: 02-May 08

## Re: converting telephone numbers to letters with all combinations

Posted 02 May 2008 - 02:29 PM

pbl, on 2 May, 2008 - 02:04 PM, said:

im keeping the numbers in the array,but unable to loop them

any help

```		// a [][] array with all the digit
char a[][] = {{'0'},	// 0 is 0 no choice
{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
---------- so on upto 9
{'w','x','y'}
};

Scanner in = new Scanner(System.in);
System.out.println("enter the telephone number");
// get phone on a String
String str = in.next();
// transalte into a char[]
char[] digit = str.toCharArray();
// scan all the digit
for(int i = 0; i < digit.lenght; i++ } {
// don't do the "-" if user put one
if(digit[i] == '-')
continue;
// translate digit into an int
int x = digit[i] - '0';
// now you can play with the
// a[x].length possible digit
for(int j = 0; j < a[x].length; j++) {
// a[x][j] will contain 1 by 1 all your possibility
}
}

```

but how to combine all combinations for eg:234,the above code gets only for individual nos say first 2 next 3 and next4
how to loop those numbers and display
im trying in forloop but unable to loop
first all nos ie 234 shud piont at 0 index ie "adg" then the index of num 4 shud increment and shud have "adh","adi"
then index of 3 shud increment and so on
may b i shud use recurence function
what do u say?

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: converting telephone numbers to letters with all combinations

Posted 02 May 2008 - 02:50 PM

veena123, on 2 May, 2008 - 02:29 PM, said:

any help

but how to combine all combinations for eg:234,the above code gets only for individual nos say first 2 next 3 and next4
how to loop those numbers and display
im trying in forloop but unable to loop
first all nos ie 234 shud piont at 0 index ie "adg" then the index of num 4 shud increment and shud have "adh","adi"
then index of 3 shud increment and so on
may b i shud use recurence function
what do u say?

If you want us to do all the work why didn't you ask for it in your first post ?

```		// a [][] array with all the digit
char a[][] = {{'0'},	// 0 is 0 no choice
{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
---------- so on upto 9
{'w','x','y'}
};

Scanner in = new Scanner(System.in);
System.out.println("enter the telephone number");
// get phone on a String
String str = in.next();
// transalte into a char[]
char[] digit = str.toCharArray();
// scan all the digit
int k = 0;
for(int i = 0; i < digit.lenght; i++ } {
// don't do the "-" if user put one
if(digit[i] == '-')
continue;
// translate digit into an int
int x = digit[i] - '0';
// now you can play with the
}
// now you have a char[][] anwer
// anwer has a length of 7
// answer[0].length is the number of possibilities for first digit
// this is a nice challenge to make a recursive method to extract all the values but I guess you can also do
for(int i0 = 0; i0 < answer[0].length; i0++) {
for(int i1 = 0; i1 < answer[1].length; i1++) {
........
........
for(int i6 = 0; i6 < answer[6].length; i6++) {
}
}
}
}
}

```

Never mention that I suggested you to do it that way.
Make a recursive method than this post will desserved its "Advance" tag.

Here is a simple recursive method

```	static void display(String str, char[][] a) {
int len = str.length();
if(len == a.length) {
System.out.println(str);
return;
}
for(int i = 0; i < a[len].length; i++)
display(str+a[len][i], a);
}

```

Edited to add the recursive method

This post has been edited by pbl: 03 May 2008 - 07:54 AM

### #7 zoom2ashish

Reputation: 0
• Posts: 1
• Joined: 21-April 09

## Re: converting telephone numbers to letters with all combinations

Posted 21 April 2009 - 01:25 PM

// PhoneNumberToString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

static int COUNT = 0;

char Letters[][4] = {
{'0'},
{'1'},
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'},
{'j', 'k', 'l'},
{'m', 'n', 'o'},
{'p', 'q', 'r', 's'},
{'t', 'u', 'v'},
{'w', 'x', 'y', 'z'}
};

void ConvertPhoneNumberToString(char phone[], int nArraySize, int nStart)
{
if(nStart < nArraySize - 1)
{
char chDigit = phone[nStart];
if(chDigit != '-')
{
int nDigit = chDigit - '0';
for(int iLetterCount=0; iLetterCount < 4; ++iLetterCount)
{
char chLetter = Letters[nDigit][iLetterCount];
if(chLetter != 0)
{
phone[nStart] = chLetter;
ConvertPhoneNumberToString(phone, nArraySize, nStart + 1);
phone[nStart] = chDigit;
}
}
}
else
{
ConvertPhoneNumberToString(phone, nArraySize, nStart + 1);
}
}
else
{
COUNT++;
printf("[%d]=> %s\n", COUNT, phone);

}
}

int _tmain(int argc, _TCHAR* argv[])
{
char phone[] = {'5','0','2','-', '8','7','6','-','2','3','4','5', '\0'};

ConvertPhoneNumberToString(phone, sizeof(phone)/sizeof(char), 8);
char ch;
scanf("%c", &ch);
return 0;
}

From,
Ashish Patel