package com.codegym.task.task22.task2202; /* Find a substring */ public class Solution { public static void main(String[] args) { System.out.println(getPartOfString("CodeGym is the best place to learn Java.")); } public static String getPartOfString(String string) { if (string == null) throw new StringTooShortException(); int startIndex = string.indexOf(" "); if (startIndex < 0) throw new StringTooShortException(); int index = 0; int endIndex = 0; int counter = 0; char[] ch = string.toCharArray(); for(char c:ch){ index++; if(c==' '){ counter++; } if(counter==5){ endIndex=index; break; } } if(index<5)throw new StringTooShortException(); if(endIndex<5)throw new StringTooShortException(); return string.substring(startIndex+1,endIndex-1); } public static class StringTooShortException extends RuntimeException { } }