Hi
Q: On a cellphone keypad, 0,1 has no characters, 2=ABC, 3=DEF and so on. So, the entered number should be converted to text. If for example 22 gives out B, 2222 gives out A. '#' sign denotes end of a character. Example: 222#22#222222 = 'CBB'. Also if another number is started new series is started. Example 2222333456622 = '2FGJNB'. Write a program to print this.
SourceCode:
package com.interview.epic;
public class KeyPad {
public static void main(String [] args)
{
String s1 ="77333777#77";
char [] c =s1.toCharArray();
String[] tokens = s1.split("#");
int k = 0;
while(k < tokens.length)
{
String tkn = tokens[k];
char [] c1 = tkn.toCharArray();
int startIndex = 0;
for(int h = 0; h < c1.length; h++)
{
if((h<c1.length-1) && (c[h] == c[h+1]))
{
continue;
}
else
{
int endIndex = h;
String s = tkn.substring(startIndex, endIndex+1);
int i = s.length();
if((s.charAt(0) != '9') && (s.charAt(0) != '7'))
{
i=i%3;
}
else
{
i=i%4;
}
int key = Integer.parseInt((Character.toString(tkn.charAt(startIndex))));
startIndex = h+1;
switch(key)
{
case (2):
print(i,"ABC");
break;
case (3):
print(i,"DEF");
break;
case (4):
print(i,"GHI");
break;
case (5):
print(i,"JKL");
break;
case (6):
print(i,"MNO");
break;
case (7):
print(i,"PQRS");
break;
case (8):
print(i,"TUV");
break;
case (9):
print(i,"WXYZ");
break;
}
}
}
k++;
}
}
public static void print(int i, String letters)
{
if(i!= 0)
{
System.out.println(letters.charAt(i-1));
}
else
{
System.out.println(letters.charAt(letters.length() -1));
}
}
}
Q: On a cellphone keypad, 0,1 has no characters, 2=ABC, 3=DEF and so on. So, the entered number should be converted to text. If for example 22 gives out B, 2222 gives out A. '#' sign denotes end of a character. Example: 222#22#222222 = 'CBB'. Also if another number is started new series is started. Example 2222333456622 = '2FGJNB'. Write a program to print this.
SourceCode:
package com.interview.epic;
public class KeyPad {
public static void main(String [] args)
{
String s1 ="77333777#77";
char [] c =s1.toCharArray();
String[] tokens = s1.split("#");
int k = 0;
while(k < tokens.length)
{
String tkn = tokens[k];
char [] c1 = tkn.toCharArray();
int startIndex = 0;
for(int h = 0; h < c1.length; h++)
{
if((h<c1.length-1) && (c[h] == c[h+1]))
{
continue;
}
else
{
int endIndex = h;
String s = tkn.substring(startIndex, endIndex+1);
int i = s.length();
if((s.charAt(0) != '9') && (s.charAt(0) != '7'))
{
i=i%3;
}
else
{
i=i%4;
}
int key = Integer.parseInt((Character.toString(tkn.charAt(startIndex))));
startIndex = h+1;
switch(key)
{
case (2):
print(i,"ABC");
break;
case (3):
print(i,"DEF");
break;
case (4):
print(i,"GHI");
break;
case (5):
print(i,"JKL");
break;
case (6):
print(i,"MNO");
break;
case (7):
print(i,"PQRS");
break;
case (8):
print(i,"TUV");
break;
case (9):
print(i,"WXYZ");
break;
}
}
}
k++;
}
}
public static void print(int i, String letters)
{
if(i!= 0)
{
System.out.println(letters.charAt(i-1));
}
else
{
System.out.println(letters.charAt(letters.length() -1));
}
}
}
No comments:
Post a Comment