Tuesday, February 14, 2012

Convert Given Message Sequence in to Corrsponding Digits

The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are mapped onto the digits as shown below. To insert the character B for instance, the program would press 22. In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ' ' should be printed to indicate a pause. For example, 2 2 indicates AA whereas 22 indicates B.



Example

hi      Case #1: 44 444
yes     Case #2: 999337777
foo bar Case #3: 333666 6660 022 2777
hello world   Case #4: 4433555 555666


Follow Up-Now Think if Question asked reverse e.g.
Given a telephone number print or produce all the possible telephone words or combinations of letters that can represent the given telephone number, then can you device an algorothm that will solve this problem efficiently. This is More Simple Then Previous One.

3 comments :

Atul anand said...

Here is the code :-

space complexity = O(1)

void ConvertToDigit(char input[])
{

int len,i,loop,k,ch,prev;
float digit;
printf("\nEncoded msg\n");
len=strlen(input);

for(i=0;i='t' && input[i]<='u') || input[i]>='w' && input[i]<='x')
{
loop=loop-1;
}
if(prev==digit)
{
printf(" ");
}
for(k=0;k<=loop;k++)
{
printf("%d",(int)digit);
}
}
else
{
printf("0 0");
}
prev=digit;
}

}

Atul anand said...
This comment has been removed by the author.
Atul anand said...

ignore above code , its incomplete:-

i dont why its not posting whole code :( :(