import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
StringBuffer sb1 = new StringBuffer("");
for (int i = 0; i <9; i++) {
sb1.append(" ");
}
sb1.append("NAME");
while(sb1.length()<34)
{
sb1.append(" ");
}
sb1.append("SOUNDEX CODE");
StringBuffer sb = new StringBuffer("");
while((input=br.readLine())!=null)
{
if(input.length()>20)
break;
int previousvalue=-1;
for (int i = 0; i <9; i++) {
sb.append(" ");
}
sb.append(input);
for (int i = 0; i <34-9-input.length(); i++) {
sb.append(" ");
}
// while(sb.length()<34)
// {
// sb.append(" ");
// }
int length=0;
char letters[]=input.toCharArray();
for (int i = 0; i < letters.length; i++)
{
switch (letters[i])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'W':
case 'H':
{
if(i==0)
{
sb.append(input.charAt(0));
}
previousvalue=-1;
break;
}
case 'B':
case 'P':
case 'F':
case 'V':
{
if(i==0)
{
sb.append(input.charAt(0));
previousvalue=1;
}
else
{
if(previousvalue!=1)
{
sb.append(1);
length++;
}
previousvalue=1;
}
break;
}
case 'C':
case 'S':
case 'K':
case 'G':
case 'J':
case 'Q':
case 'X':
case 'Z':
{
if(i==0)
{
sb.append(input.charAt(0));
previousvalue=2;
}
else
{
if(previousvalue!=2)
{
sb.append(2);
length++;
}
previousvalue=2;
}
break;
}
case 'D':
case 'T':
{
if(i==0)
{
sb.append(input.charAt(0));
previousvalue=3;
}
else
{
if(previousvalue!=3)
{
sb.append(3);
length++;
}
previousvalue=3;
}
break;
}
case 'L':
{
if(i==0)
{
sb.append(input.charAt(0));
previousvalue=4;
}
else
{
if(previousvalue!=4)
{
sb.append(4);
length++;
}
previousvalue=4;
}
break;
}
case 'M':
case 'N':
{
if(i==0)
{
sb.append(input.charAt(0));
previousvalue=5;
}
else
{
if(previousvalue!=5)
{
sb.append(5);
length++;
}
previousvalue=5;
}
break;
}
case 'R':
{
if(i==0)
{
sb.append(input.charAt(0));
previousvalue=6;
}
else
{
if(previousvalue!=6)
{
sb.append(6);
length++;
}
previousvalue=6;
}
break;
}
}
}
if(length<3)
{
while(length!=3)
{
sb.append(0);
length++;
}
}
else if(length>3)
sb=sb.delete(sb.length()-length+3,sb.length());
sb.append("\n");
}
System.out.println(sb1);
System.out.print(sb);
for (int i = 0; i <19; i++) {
System.out.print(" ");
}
System.out.println("END OF OUTPUT");
}
}
0 comments: