Michał Lewaszow
Michał Lewaszow

Reputation: 41

String reverse, wrong results

So my problem is about getting reversed String but only words which are longer or equals 5 characters. So if we pass ("Hey fellow warriors") to the method we should get "Hey wollef sroirraw" in return. And my code gives me some weird results which are = "sroirraw fellow warriors ".

Here is my code, please give me some clue. The whitespace after last word shouldn't be returned but I don't know why it is.

public class PrimeChecker {

public String spinWords(String sentence) {

    String[] tablica = sentence.split(" ");

    for ( String x : tablica ) {
        int y = 0;
        if (  x.length() >= 5 ) {
            StringBuilder p = new StringBuilder(x).reverse();
            x = p.toString();
        }
        tablica[y] = x;
        y++;            
    }

    StringBuilder wynik = new StringBuilder();

    for ( String z : tablica ) {
        int y = 0;
        tablica[y] = z;
        wynik.append(tablica[y]);
        if (tablica.length > 1 && y != tablica.length - 1 ) {
            wynik.append(" ");
        }
        y++;
    }
    return wynik.toString();
}   
}

Tester

public class PrimeCheckerTest {
    public static void main(String[] args) {
        PrimeChecker obiekt = new PrimeChecker();
        System.out.println(obiekt.spinWords("Hey fellow warriors").toString());     
    }
}

Upvotes: 3

Views: 138

Answers (4)

gonzajf
gonzajf

Reputation: 26

You can do it in a simpler way:

public String spinWords(String sentence) {

    String[] tablica = sentence.split(" ");

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

        if(tablica[i].length() >= 5) {
            StringBuilder p = new StringBuilder(tablica[i]).reverse();
            tablica[i] = p.toString();
        }
    }

    StringBuilder builder = new StringBuilder();
    for(String s : tablica) {
        builder.append(s + " ");
    }
    String str = builder.toString();

    return str;
}   

Upvotes: 0

Mohammad
Mohammad

Reputation: 739

you need to define y out of for loop and before the second loop give it the value of zero , because you reset the value of y each loop .

Upvotes: 0

Ayesha Ahmad
Ayesha Ahmad

Reputation: 31

This code below will work. You have added y=0 inside the loop. That is unnecessary.

public class PrimeChecker {

public String spinWords(String sentence) {

String[] tablica = sentence.split(" ");
int y = 0;
for ( String x : tablica ) {

    if (  x.length() >= 5 ) {
        StringBuilder p = new StringBuilder(x).reverse();
        x = p.toString();
    }
    tablica[y] = x;
    y++;            
}

StringBuilder wynik = new StringBuilder();
y=0;
for ( String z : tablica ) {

    tablica[y] = z;
    wynik.append(tablica[y]);
    if (tablica.length > 1 && y != tablica.length - 1 ) {
        wynik.append(" ");
    }
    y++;
}
return wynik.toString();
}   
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201437

First, I would prefer to split with \\s+ which matches one or more white-space characters. Second, I would use a lambda with Arrays.stream on the tokens I split. Then I would map each word, reversing every word with 5 or more characters. That can be done with a StringBuilder and reverse(). And since this method doesn't need any instance state we can make it static. Finally, join the words backs together with a Collector. Like,

public static String spinWords(String sentence) {
    return Arrays.stream(sentence.split("\\s+"))
            .map(s -> s.length() >= 5 ? new StringBuilder(s).reverse().toString() : s)
            .collect(Collectors.joining(" "));
}

And to test it

public static void main(String[] args) {
    System.out.println(spinWords("Hey fellow warriors"));
}

Which gives (as specified)

Hey wollef sroirraw

Upvotes: 2

Related Questions