5.1k views
3 votes
Write code which takes a user input of a String and an integer. The code should print each letter of the String the number of times the user inputted in reverse order. I believe it's supposed to use nested loops, but I can only get it to repeat the whole word backwards x times.

ex.
Input a String:
code
Input an integer:
3
eeedddoooccc

1 Answer

4 votes

import java.util.*;

public class myClass {

public static void main(String args[]) {

Scanner scan = new Scanner(System.in);

System.out.print("Input a String: ");

String str = scan.nextLine();

System.out.print("Input an integer: ");

int num = scan.nextInt();

for(int i=str.length()-1; i>=0; i--) {

for(int j=0; j<num; j++) {

System.out.print(str.charAt(i));

}

}

}

}

User KJ Saxena
by
5.9k points