#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    [Homework] Need some help with making a translator in java.


    Hello all,

    I am currently working on making an ubbi dubbi translator. Currently I have it so that I can add ub to the beginning of vowels, but there are certain clusters that we must follow that only one ub would be in front of it, they are oa, oo, ou, ea, ee, ie, ei, ai, and oe. So cool, would be: cubool, instead of cuboubol. My only current problem is trying to make it so that only one ub will appear in front of those vowel clusters. I should also note that we cannot use string builder, arrays, or regex for this.

    Here is my current ubbi dubbi code:
    Code:
    public static String translateFromEnglish(String phrase) { 		
        if (phrase == null) { 			
        return null; 		
        } 		
        String result = ""; 		
        String ub = "ub"; 		
        for (int i = 0; i < phrase.length(); i++) { 		
    	char c = phrase.charAt(i); 		
    	if (c == 'a') { 			
    	result += ub; 			
    	result += phrase.charAt(i); 		
    	} else if (c == 'o') { 			
    	result += ub; 				
            result += phrase.charAt(i); 		
    	} else if (c == 'e') { 			
    	result += ub; 			
    	result += phrase.charAt(i); 			
            } else if (c == 'i') { 			
     	result += ub; 			
    	result += phrase.charAt(i); 		
    	} else if (c == 'u') { 			
    	result += ub; 			
    	result += phrase.charAt(i); 		
    	} else { 			
    	result += c; 			
            } 	
        } 		
        return result; 	
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    My only current problem is trying to make it so that only one ub will appear in front of those vowel clusters.
    What hinders you from checking whether the previous letter were vowel or not?

    I should say your code is bad. Too many copies of the same functionality - that prevents you from easily modify it to your needs.

    At first use logical AND with your conditions:

    Code:
    boolean prevWasVowel = false;
    for (...) {
        boolean curIsVowel = false;
        if (c == 'a' || c == 'o' || c == 'e' ...) {
            curIsVowel = true;
        }
        if (curIsVowel && !prevWasVowel) {
            // ubify
        }
        prevWasVowel = curIsVowel;
    }
    we cannot use string builder, arrays, or regex for this.
    I should say that though restriction on regex could be understood, the restriction of StringBuilder and arrays is stupid enough from technical point of view.

    Each time when you do "a += b" on strings new string is created in memory and both strings are copied into it - so this approach becomes slower with the growing of string length.
    Last edited by rodiongork; November 25th, 2013 at 01:11 AM.
    CodeAbbey - programming problems for novice coders
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by rodiongork
    What hinders you from checking whether the previous letter were vowel or not?

    I should say your code is bad. Too many copies of the same functionality - that prevents you from easily modify it to your needs.

    At first use logical AND with your conditions:

    Code:
    boolean prevWasVowel = false;
    for (...) {
        boolean curIsVowel = false;
        if (c == 'a' || c == 'o' || c == 'e' ...) {
            curIsVowel = true;
        }
        if (curIsVowel && !prevWasVowel) {
            // ubify
        }
        prevWasVowel = curIsVowel;
    }

    I should say that though restriction on regex could be understood, the restriction of StringBuilder and arrays is stupid enough from technical point of view.

    Each time when you do "a += b" on strings new string is created in memory and both strings are copied into it - so this approach becomes slower with the growing of string length.
    Thanks for the help! I know it's stupid how we can't use string builder and arrays, but we haven't covered them in class yet, and this is the last assignment we aren't allowed to use them.

IMN logo majestic logo threadwatch logo seochat tools logo