You got very close. You already had the idea to print the substring from 0 to i
. Then you just need an inner loop that starts at i+1
and loops until word.length
and print out the char at i
. Also you need to use System.out.print()
so that they will be on the same line:
Scanner sc=new Scanner(System.in);
System.out.println("What will your string be?");
String word=sc.next();
for(int i=0;i<word.length();i++) {
System.out.print(word.substring(0,i+1));
for(int j = i+1; j < word.length(); j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
Output:
What will your string be?
example
eeeeeee
exxxxxx
exaaaaa
exammmm
examppp
exampll
example
solved Word advancing in letters each new line