Question - LINK
/*
* @author Divyanshu
* @mail divyanshu17x@gmail.com
* @place GNDU RC SATHIALA
* @date 27 / 07 / 2014
* @judge uva.onlinejudge.org
* @verdict Accepted
* @runtime 0.145
* @problem 10082 WERTYU
* @problemType Keyboard Simulation
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Main {
// the characters of keyboard as shown in image of question
static char[] keyboardArray = {'`','1','2','3','4','5','6','7','8','9','0','-','=',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']','\\',
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';','\'',
'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/'};
public static void main (String args[]) throws Throwable
{
String line ;
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
//read the line
while((line = in.readLine()) != null )
{
// a temporrary string variable for outputting result
String temp = "";
//loop through all characters of input string
for(int i = 0 , length = line.length() ; i < length ; ++i )
{
//if the character is a space , add a space in temp
if(line.charAt(i) == ' ')
{
temp += " " ;
}
// else if a newline , add in temp
else if(line.charAt(i) == '\n')
{
temp += "\n" ;
}
//else add the required character in temp ,positioned at index one place left to the given character
else
{
// search and save the index of character from keyboard array
int index = searchIndex(line.charAt(i)) ;
// add the character to temp one place left of the current character
temp += keyboardArray[index-1];
}
}
System.out.println(temp);
}
}
//searches the index of character present in keyboardArray
// @params i is the index is index of character in keyboardArray
private static int searchIndex(char ch) {
for(int index = 0 , length = keyboardArray.length; index < length ; ++index)
{
if( keyboardArray[index] == ch )
return index;
}
return 0;
}
}
No comments:
Post a Comment