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]
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.
CodePudding user response:
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;
}
}
CodePudding user response:
Here is another possibility that uses the existing array of characters.
- create the array using
String.toCharArray()
- iterate half-way thru the array, simply swapping the end characters moving inward.
- then since the String constructor takes an array of characters as an argument, return
new String(array)
public static String reverse(String s) {
char[] array = s.toCharArray();
for (int i = 0; i <array.length/2; i ) {
char ch = array[i];
array[i] = array[array.length-i-1];
array[array.length-i-1] = ch;
}
return new String(array);
}
Note that for even length strings, the middle two are swapped. For odd length strings, the middle one is untouched.