The below code doesn't take into account the fact that keys 7 and 9 represent 4 alphabets and also it just prints ' ' and '+' for key 1 and 0 respectively. The code is just to explain the logic for printing the combinations and additional logic can be added to handle these cases.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char digitToChar(char x, int index)
{
char ch;
switch(x)
{
case '0':
ch = '+'; break;
case '1':
ch = ' '; break;
case '2':
ch = "abc"[index]; break;
case '3':
ch = "def"[index]; break;
case '4':
ch = "ghi"[index]; break;
case '5':
ch = "jkl"[index]; break;
case '6':
ch = "mno"[index]; break;
case '7':
ch = "pqrs"[index]; break;
case '8':
ch = "tuv"[index]; break;
case '9':
ch = "wxyz"[index]; break;
default:
ch = ' ';
}
return ch;
}
void printAll(char* num, char* arr, int len, int x)
{
int i=0;
if(x == len)
{
printf("%s\n", arr);
return;
}
for(i=0; i<3; i++)
{
arr[x] = digitToChar(num[x], i);
printAll(num, arr, len, x+1);
}
}
int main()
{
char* num = "8017393450";
char arr[20] = {0,};
printAll(num, arr, 10, 0);
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char digitToChar(char x, int index)
{
char ch;
switch(x)
{
case '0':
ch = '+'; break;
case '1':
ch = ' '; break;
case '2':
ch = "abc"[index]; break;
case '3':
ch = "def"[index]; break;
case '4':
ch = "ghi"[index]; break;
case '5':
ch = "jkl"[index]; break;
case '6':
ch = "mno"[index]; break;
case '7':
ch = "pqrs"[index]; break;
case '8':
ch = "tuv"[index]; break;
case '9':
ch = "wxyz"[index]; break;
default:
ch = ' ';
}
return ch;
}
void printAll(char* num, char* arr, int len, int x)
{
int i=0;
if(x == len)
{
printf("%s\n", arr);
return;
}
for(i=0; i<3; i++)
{
arr[x] = digitToChar(num[x], i);
printAll(num, arr, len, x+1);
}
}
int main()
{
char* num = "8017393450";
char arr[20] = {0,};
printAll(num, arr, 10, 0);
return 0;
}
No comments:
Post a Comment