Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reverse the order of characters within words with five letters or more

I’m new to Java and I’m still learning. As a part of my learning I took a challenge to write a program that will take a string, and check if there are any words, that are 5 or more in length and if so, it will flip the chars in the 5+ long words. The challenge was to do it without using Collections.reverse(list) or StringBuilder.

My idea was, I’d say simple to write 2 methods.
Main will split the string, and iterate by each string array index looking for 5+ long words, and method called ‘reverse’ would be triggered inside ‘if’ condition. Look like this one works okay.
For the reverse method idea was to split the word to an array, and then with help of 2 nested ‘for’ loops iterate by indexes of ‘ori’ and auxiliary ‘rev’ arrays to assign value of index ori[i] to index rev[j].

So beginning with i=0 and j=arr.length-1 will result with assigning value "s" out of sentence word in ori[0] to rev[7], then "e" from ori[1] to rev[6], and so on, so the result would be [e, c, n, e, t, n, e, s]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Instead I get output like this:

[This, [e, e, e, e, e, e, e, e], will, [n, n, n, n, n, n, n], [s, s, s, s, s], with, more, than, five, [., ., ., ., ., ., ., .]]

I tried to fix that many ways, but so far my logic fails. Could someone explain me please, what am I doing wrong, how did I screw that apparently simple logic?

I’ll figure out how to display this as a regular sentence without square brackets and commas later so this is no issue so far.

The code:

import java.util.Arrays;

public class Main {
    public static void main (String[] args) {

        String original = "This sentence will contain words with more than five letters.";
        String[] splitter = original.split(" ");
        for (int i = 0; i < splitter.length; i++){
            if (splitter[i].length() >= 5){
                splitter[i] = reverse(splitter[i]);
            }
        }
        System.out.println(Arrays.toString(splitter));

    }
    public static String reverse (String s){
        String [] ori = s.split("");
        String [] rev = new String[ori.length];
        for (int i = 0; i < rev.length; i++) {
            for (int j = rev.length-1; j >=0; j--) {
                rev[j] = ori[i];
            }
        }
        s = Arrays.toString(rev);
        return s;
    }
}

Please be understading for a newbie 🙂

I tried to modify this part:

public static String reverse (String s){
        String [] ori = s.split("");
        String [] rev = new String[ori.length];
        for (int i = 0; i < rev.length; i++) {
            for (int j = rev.length-1; j >=0; j--) {
                rev[j] = ori[i];

by swapping i/j, rev[i] = ori[j], –j, j > 0 and many others, mostly blind shots looking for some inspiration where my logic fails.

>Solution :

Hy Pawel Niewolak, the issue is with your reverse function.

Depending on your requirements use that:

 public static String reverse(String s) {
        String[] ori = s.split("");
        String[] rev = new String[ori.length];

        for (int i = 0; i < rev.length; i++) {
            rev[i] = ori[rev.length - i - 1];
        }
        s = "";
        for (String str : rev) {
            s += str;
        }
        return s;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading