Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Anthony Cosentini
- // Course: CIS1068 Section 1
- // Date: 10/13/2008
- // Homework submission for homework 6.
- import java.util.Scanner;
- public class Lab6 {
- public static String getNysiis(String s) {
- s = s.toUpperCase();
- String t = "";
- // If the last character is an 'A' or an 'S' remove it...
- if (s.charAt(s.length() - 1) == 'A' || s.charAt(s.length() -1) == 'S')
- {
- s = s.substring(0, s.length()-1);
- }
- // Do the first part of the name...
- int firstPartLength = 0;
- if (s.substring(0,3).equals("MAC")) {
- t += "MCC";
- firstPartLength = 3;
- } else if (s.substring(0,2).equals("KN")) {
- t += "NN";
- firstPartLength = 2;
- } else if (s.substring(0,1).equals("K")) {
- t += "C";
- firstPartLength = 1;
- } else if (s.substring(0,2).equals("PH")) {
- t += "FF";
- firstPartLength = 2;
- }
- // Figure out the end of the word...
- String endOfWord = s.substring(s.length()-2, s.length());
- boolean trigger = true;
- boolean secondTrigger = false;
- boolean useEnd = false;
- char end = 'D';
- if (endOfWord.equals("EE"))
- {
- end = 'Y';
- trigger = false;
- }
- if (endOfWord.equals("IE")) {
- end ='Y';
- trigger = false;
- }
- if (endOfWord.equals("DT") || endOfWord.equals("RT") || endOfWord.equals("RD") || endOfWord.equals("NT") || endOfWord.equals("ND")) {
- end ='D';
- trigger = false;
- }
- if (endOfWord.equals("AY")) {
- end ='Y';
- trigger = false;
- }
- if (trigger == true) {
- useEnd = true;
- }
- trigger = false;
- int middleLength = 0;
- if (useEnd == true) {
- middleLength = s.length();
- }
- else {
- middleLength = s.length()-2;
- }
- // Now the middle...
- for(int i = firstPartLength; i < middleLength; i++) {
- boolean lastResort = true;
- if (secondTrigger == true) {
- System.out.println("I: " + i);
- secondTrigger = false;
- continue;
- }
- if (trigger == true) {
- System.out.println("I: " + i);
- trigger = false;
- secondTrigger = true;
- continue;
- }
- if (i < s.length() - 2) {
- String temp = s.substring(i, i+3);
- if (temp.equals("SCH")) {
- t += "SSS";
- trigger = true;
- }
- }
- if (i < s.length() - 1) {
- String temp = s.substring(i, i+2);
- if (temp.substring(0,2).equals("PH")) {
- lastResort = false;
- t+= "FF";
- }
- if (temp.substring(0,2).equals("KN")) {
- lastResort = false;
- t+= "NN";
- }
- else if (temp.substring(0,1).equals("K")) {
- lastResort = false;
- t+= "C";
- }
- if (temp.substring(0,2).equals("EV")) {
- lastResort = false;
- t+= "AF";
- }
- else if (temp.substring(0,1).equals("A")) {
- lastResort = false;
- t+= "A";
- }
- else if (temp.substring(0,1).equals("E")) {
- lastResort = false;
- t+= "A";
- }
- else if (temp.substring(0,1).equals("I")) {
- lastResort = false;
- t+= "A";
- }
- else if (temp.substring(0,1).equals("O")) {
- lastResort = false;
- t+= "A";
- }
- else if (temp.substring(0,1).equals("U")) {
- lastResort = false;
- t+= "A";
- }
- }
- String temp = s.substring(i, i+1);
- if (temp.substring(0,1).equals("Q")) {
- lastResort = false;
- t+= "G";
- }
- if (temp.substring(0,1).equals("Z")) {
- lastResort = false;
- t+= "S";
- }
- if (temp.substring(0,1).equals("M")) {
- lastResort = false;
- t+= "N";
- }
- if (temp.substring(0,1).equals("H")) {
- if (isNotVowel(s.charAt(i-1)) || isNotVowel(s.charAt(i+1))) {
- t+= s.charAt(i-1);
- lastResort = false;
- }
- }
- if (temp.substring(0,1).equals("W")) {
- if (!isNotVowel(s.charAt(i-1))) {
- t+= s.charAt(i-1);
- lastResort = false;
- }
- }
- if (lastResort == true) {
- if (t.length() > 0) {
- if (s.charAt(i) != t.charAt(t.length()-1)) {
- t+= s.charAt(i);
- }
- }
- else {
- t+=s.charAt(i);
- }
- }
- }
- // Add the end that we calculated before...
- if (useEnd == false) {
- t+=end;
- }
- // Return our string...
- return t;
- }
- // This method check to see if the character passed to it is not a vowel....
- public static boolean isNotVowel(char character) {
- if (character != 'A' && character != 'E' && character != 'I' && character != 'O' && character != 'U')
- return true;
- else {
- System.out.println("WE HAVE A VOWEL: " + character);
- return false;
- }
- }
- public static void main(String args[]) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter your word to convert to NYSIIS: ");
- String word = scan.nextLine();
- System.out.println("Result: " + getNysiis(word));
- }
- }
Add Comment
Please, Sign In to add comment