0

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.

Riemann
  • 5
  • 4

2 Answers2

1

You won't need two loops for this. You can use the substring property. If you really want to use another loop, you can ofcourse substitute the substringwith a loop as in your answer, And a simple if condition to make sure we are not going past the last index of the input.

    String str = "hello!";
    int len = str.length();
    int gap = 2; // This determines the consecutive character size.
    for( int set = 0; set <= len; set++ )
    {
        if( ( set + gap ) <= len )
        {
            String subString = str.substring( set, set + gap );
            System.out.println( subString );
        }
    }

This is how you could use a second loop instead of the substring

    for( int set = 0; set <= len; set++ )
    {
        if( ( set + gap ) <= len )
        {
            String result = "";
            for( int i = set; i < set + gap; i++ )
            {
                result += str.charAt( i );
            }
            System.out.println( result );
        }
    }

If you are using string concatenation inside a loop. Keep in mind that it is not recommended. Use a StringBuilder instead. For your scenario, both these approaches will work.

Klaus
  • 1,641
  • 1
  • 10
  • 22
0

Just wanted to point out that your original code was very close to being correct. This version produces the desired result:

String str = "hello!";
int len = str.length();
for (int set = 2; set <= (len - 1); set++) {
    for (int n = 0; n <= (len - set); n++) {     // changed (len - i) to (len-set)
        for (int k = 0; k < set; k++) {          // changed (k <= n)  to (k < set)
            System.out.print(str.charAt(n+k));   // changed (k)       to (n+k)
        }
        System.out.println();
    }
}

Following Klaus's suggestion, you could also use substring:

for(int set = 2; set < len; set++) {
    for(int n=0; n <= len-set; n++) {
        System.out.println(str.substring(n, n+set));
    }
}
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16