2

Basically I am trying to get my code to print all substrings of a input. If the input is rum it would go as r u m ru um rum. I am not very good with nested loops and so far the out put I get is rr uu mm. I would appreciate if anyone can help me understand what I'm doing wrong here.

import java.util.Scanner;
    
public class NestedLoop {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        
        for(int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            for (int j = 1; j < s.length(); j ++) {
                char cu = s.charAt(j);
                System.out.println(cur);
            }
        }
    }
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • 1
    You're almost there. Try and make j = i + 1 in your second for loop – davidalayachew Apr 25 '22 at 19:46
  • 1
    Also, print the substring, not just the single character. Look at your `System.out.println()` – davidalayachew Apr 25 '22 at 19:47
  • Your all substrings will have length from 1 to `s.length()` In the outer loop you can increment length of substring and in the nested print substring (just call `substring` method) with current length, starting from index which will be changed in the nested loop. Additionally you have to check if your substring ends "in range" of string. – Matt Apr 25 '22 at 19:58
  • The outer loop should count for the possible lengths (same as the length of the sting, start at 1, end at length). The inner loop should count the starting character index (same as the length, start at 0, end when j + i > length). Inside the loops I would work with `String.substring(int, int)`. – cyberbrain Apr 25 '22 at 20:00

2 Answers2

0

May this can help you.

You will end up in 2^(String length) combinations as your final output including the empty string. You can avoid it by starting the outer loop from 1 instead of 0. The outer loop is counting possible outputs and inter loop in bit counter with a upper limit as a length of the string. Based on bit set for j in binary the corresponding character is selected.

public static List<String> findSubstrings(String str) {
    int charCount = str.length();
    int powSize = (int)Math.pow(2, charCount);
    List<String> result = new ArrayList<>();

    for (int i = 0; i < powSize; i++) {
      StringBuilder sb = new StringBuilder();
      for (int j = 0; j < charCount; j++) {
        if ((i & (1 << j)) != 0) {
          sb.append(str.charAt(j));
        }
      }
      result.add(sb.toString());
    }

    return result;
}
vcosk
  • 2,894
  • 2
  • 23
  • 23
0

The second pointer should start from 1 plus the outer loop . To print the string you can use the substring function which includes the element at the starting index and excludes the element at the end index.

public class Nestedloop {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();

        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j <= s.length(); j++) {
                System.out.println(s.substring(i, j));
            }
        }
    }
}

and the output for rum is

r
ru
rum
u
um
m
Gurkirat Singh Guliani
  • 1,009
  • 1
  • 4
  • 19