// this code should generate all possible eight-letter combinations from an eight-digit phone number. The procedure doesn't check if the phone number contains 0's or 1's.
Code:
#include<iostream>
using namespace std;
const char* letters[] = {
"ABC",
"DEF",
"GHI",
"JKL",
"MNO",
"PRS",
"TUV",
"WXY"
};
FILE* fp;
char str1[15];
void GenCombinations (
const char* number,
char str[])
{
if (*number == '\0')
{
fwrite(str - 8, 1, 8, fp);
cout<<str<<endl;
fwrite("\r\n", 1, 2, fp);
return;
}
else if (*number == '-')
GenCombinations(number + 1, str);
else {
const int group = *number - '0' - 2;
for (int i = 0; i < 3; i++)
{
*str = letters[group][i];
cout<<str<<endl;
GenCombinations(number + 1, str + 1);
}
}
}
int main(){
char str2[]="42472888";
strcat("\0",str2);
GenCombinations(str2,str1);
cout<<str1<<endl;
system("PAUSE");
return 0;
}