Spaces:
Running
Running
File size: 1,113 Bytes
78bbc3c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
Write a program that asks the user to enter a string.
The program will then print a triangle of letters corresponding to the string, as shown in the sample printouts below.
Example output:
Give a string: hello
h
he
hel
hell
hello
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
final Random r = new Random();
//ADD
Scanner reader= new Scanner(System.in);
System.out.print("Give a string: ");
String user_str = String.valueOf(reader.nextLine());
int wordLastIdx = user_str.length() - 1;
for (int i = 0; i <= wordLastIdx; i++) {
System.out.println(user_str.substring(0, i+1));
}
}
}
Testing with input abcdef
Give a string: abcdef
a
ab
abc
abcd
abcde
abcdef
Testing with input hi
Give a string: hi
h
hi
Testing with input xxxxx
Give a string: xxxxx
x
xx
xxx
xxxx
xxxxx
Testing with input goodevening
Give a string: goodevening
g
go
goo
good
goode
goodev
goodeve
goodeven
goodeveni
goodevenin
goodevening
|