This is my first question in stack overflow so forgive me if I am going against the rules.
I want to take user input as string and reverse every nth word of the string. The n value is also entered by the user. If the user inputs invalid value the program should respond the user accordingly. Until now, I am able to take a string input and reverse the whole string. I need help to take nth value as an input and reverse the string with the n value. And the program should run without using "String.reverse" or any other string function. Hopefully, I elaborated every aspect of the problem. Thanks :)
Sample I/O should look like:
User Input = "Hello World! Programming is fun"
User Inputs n value = "2"
Expected Output = "Hello !dlroW Programming si fun"
User inputs n-value "2", which means every second word should be reversed.
Following is the program that I wrote till now:
import java.util.*;
public class StringReverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String original, reverse="";
System.out.print("Please enter a sentence to reverse: ");
original= in.nextLine();
for (int i=original.length()-1; i>=0; i--) {
reverse += original.charAt(i);
}
System.out.println(reverse);
}
}