I need to accomplish the following in Java:
1. Take a string.
2. Generate all 2-consecutive characters in the string.
3. Generate all 3-consecutive characters in the string.
4. So on and so forth until the last generation, which will be ( str.length() )-1-consecutive characters.
To clarify further, consider the string hello!
. The string has length 6
. Notice how the last generation is that of 5-consecutive characters. The output ought to be:
he // 2-consecutive
el // 2-consecutive
ll // 2-consecutive
lo // 2-consecutive
o! // 2-consecutive
hel // 3-consecutive
ell // 3-consecutive
llo // 3-consecutive
lo! // 3-consecutive
hell // 4-consecutive
ello // 4-consecutive
llo! // 4-consecutive
hello // 5-consecutive
ello! // 5-consecutive
This is what I tried:
String str = "hello!";
int len = str.length();
for (int set = 2; set <= (len-1); set++) {
for (int n = 0; n <= (len-1); n++) {
for (int k = 0; k <= n; k++) {
System.out.print(str.charAt(k));
}
System.out.println();
}
}
The code gives the following output, which is completely different from what I had earlier wished for :
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
This question would help me but I am totally not conversant with ruby.