For example, string = "ABCDEFGH". if n=2, then output should be "BADCFEHG"; if n=3, output should be "CBAFEDHG". I know i should use stringbuilder to reverse, but how can i split the string to n parts and then reverse each part?
-
1Welcome on SO. You should do some research before posting a question/requirement on StackOverflow. See [ask] please, and edit your question with some of your attempts – AxelH Sep 06 '17 at 12:55
-
Possible duplicate of [Reverse a string in Java](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – pedromss Sep 06 '17 at 12:56
-
@pedromss I don't think it is a direct duplicate, this one works on groups of N letters rather than individual letters – jrtapsell Sep 06 '17 at 13:11
3 Answers
I will not give you a code for this, you need to learn by trying.
Do this requirement step by step :
- how to read that
String
block by block :String.substring(int, int)
in a loop. You need to understand how to calculate the boundery of each blocks - how to reverse a
String
: see about Reverse a String in JAVA - create the output by mixing those two steps.
Don't try to do everything in once. Find how to do those two separatly, then put the logic together. This is quite simply in the end.

- 14,325
- 2
- 25
- 55
String newStr="";
String oldStr = "ABCDEFGH";
for(int i =0; i<oldStr.length();i+=n) {
if(i+n >= oldStr.length()){
n = oldStr.length()-i;
}
newStr += new StringBuilder(oldStr.substring(i,i+n)).reverse().toString();
}
Edit: Sorry for missreading your question, this little loop does what you're asking for!
What we are doing here is making oldString.length() / n
iterations to split the String in n portions. Because the length might not be dividable by your n we have to check if i+n
wont be larger than the length of your word (eventually creating a IndexOutOfBoundsException
). If this is the case we just set n
so that it adds to i to the rest of the word. Hope that explains it well.

- 511
- 3
- 20
-
And for the rest ? This is incomplet, you only reverse the first block of N character. – AxelH Sep 06 '17 at 12:59
-
@AxelH Sorry missread your question, checkout the edit! You can of course shortcut the declaration of newString1 & 2, made it this way for readability. – Claudio Brasser Sep 06 '17 at 13:02
-
Not my question ... but you are still incorrect, "ABCDEF" should output for a block of 2 "BADCFE" not " BACDEF". You need to iterate the `String` to reverse each block, not the first block and the rest of the `String` – AxelH Sep 06 '17 at 13:05
-
-
1Now, you can explain your logic to this learner ... (and indent your code). This is always better to provide explanation. – AxelH Sep 06 '17 at 13:21
I've given you most of the code but it's unfinished. You will have to understand what I left out and how to fix it to complete the problem.
String originalString = someString; //String from user
String tempString = ""; //String used for temporary reasons
String finalString = ""; //Your end result
int n = someNumber; //Number from user
//Loops through the original string, incrementing by n each time
for (int i = 0; i < originalString.length() - n; i += n)
{
//Gives us the current substring to reverse
tempString = originalString.substring(i, i + n);
//Starts at end of substring and adds each char to the final string
for (j = n - 1; j >= 0; j--)
{
finalString += tempString.charAt(j);
}
}

- 29
- 4