1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <string>
using namespace std;

char LetterMap[10][5] = 
{
    " ",    // 0
    "",     // 1
    "abc",  // 2
    "def",  // 3
    "ghi",  // 4
    "jkl",  // 5
    "mno",  // 6
    "pqrs", // 7
    "tuv",  // 8
    "wxyz"  //9
};

void LetterCombinations(string& digits, string letters)
{
    if (digits.size() == letters.size())
    {
        cout << letters << '\n';
        return;
    }
    int num = digits[letters.size()] - '0';
    char * letter = LetterMap[num];

    while(*letter != '\0')
    {
        LetterCombinations(digits, letters + *letter);
        ++ letter;
    }
}

int main(int argc, char** argv)
{
    string dig1 = "7";
    string dig2 = "23";
    string dig3 = "234";
	
	cout << dig1 <<":\n";
    LetterCombinations(dig1, "");
	cout << endl;
	
	cout << dig2 <<":\n";
    LetterCombinations(dig2, "");
	cout << endl;
	
	cout << dig3 <<":\n";
    LetterCombinations(dig3, "");
	cout << endl;
	
    return 0;
}
View Program Text


Test Status