Goran Shakeit Goran Shakeit - 18 days ago 7
Java Question

How to create a loop for "rövarspråket", more info about "rövarspråket" below

I need to create a loop called "rövarspråket":
https://en.wikipedia.org/wiki/R%C3%B6varspr%C3%A5ket

But the problem is that it doesn't translate correctly and specially when I translate a name the output is just 3 letters.

To check if the translation is correct I use:
http://rövarspråket.se/

consonantsx = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"; //Saves all consonants to string
char consonants[] = consonantsx.toCharArray(); //String to charr

System.out.println("Mata in en mening"); //Input of what you want to translate
skriv = new Scanner(System.in);
input = skriv.nextLine(); //Saves the input
length = input.length(); //Length inc. space
char array[] = input.toCharArray(); // Input to a char array

slang = "";
System.out.println("På rövarspråk:");
for(int i = 0; i<length; i++) {
for(int x = 0; x<20; x++){
if(array[i] == consonants[x])
{
add = array[i]+"o"+array[i];
slang = slang + add;
break;
}
else{
}
}
}
System.out.println(slang);

Answer

so the idea is to dublicate consonants and put "o" between them, like t becomes tot, s becomes sos. Vocals are just copied. So you need a method that tells you if a given character is a vocal or consonant to base your decision on that.

public static boolean isConsonant(char inputChar){

    final String consonantsx = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"; 
    char consonants[] = consonantsx.toCharArray(); // String to charr

    for(int i=0; i < consonants.length;i++){
        if(inputChar == consonants[i]){ //note that in Strings u use the equals method instead of "=="
            return true;
        }
    }

    return false;
}

Given this method you can use it in the "translator method".

    public String rovarSpraket(String normalString) {

    char[] array = normalString.toCharArray(); // Input to a char array

    System.out.println("På rövarspråk:");

    String slang = "";

    for (int i = 0; i < normalString.length(); i++) {

        String add = "" + array[i];

        if(Goran.isConsonant(array[i])){

            add += "o" + array[i];

        }

        slang += add;

        }

    return slang;
}

This translates stubborn to sostotubobboborornon like in the wikipedia article https://en.wikipedia.org/wiki/R%C3%B6varspr%C3%A5ket.