/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        // need to create the sample into a character array as oppose to String array!
        String sample = "the quick brown fox jumps over the lazy dog";
        
        int lengthLine = 10;
        
        char [][] finalOutput = new char [5][lengthLine];  // max 10 chars per line
        
        stringExercise  se = new stringExercise(sample, lengthLine);
        
            }
}

class stringExercise
{
    private String sample;
    private int lengthLine;
    char [] sampleChars = sample.toCharArray();
    
    public stringExercise(String sample, int lengthLine)
    {
        this.sample=sample;
        this.lengthLine=lengthLine;
        
        System.out.println(checkString());
        
    }
    
    
    public String checkString()
    {
        
        
        //no way to break the text;
        if ((sampleChars.length>lengthLine) &&  (sample.indexOf(" ")==-1))
            {
                System.out.println("contigious text longer than limit: " + lengthLine);
                return null;
                
            }
        
        //if the string is less than or equal segment specified, it will return entire string     
        if (sampleChars.length<=lengthLine)
        {
            System.out.println("contigious text shorter or equal to limit: " + lengthLine);
            return sample;
            
        }
        
        
        // if the segment is greater or equal to segment and there is a space in text....
        // most processing will occur here
        
        double stringsRequired;
        
        stringsRequired=Math.ceil(sample.length()/lengthLine);  // to get approximation number strings
        int stringsRequiredConverted = (int) stringsRequired;
        System.out.println("Approximate number strings: " + stringsRequiredConverted);
        
        if ((sampleChars.length>=lengthLine) &&  (sample.indexOf(" ")!=-1))
            {
                System.out.println("should reach here");
               
                String output[] =  new String[stringsRequiredConverted];
                
                int storePos=0;
                int charsToRewrite=0;
                
                //char[] charArray = null;
                
                int segment = 0;
                
                
                    for (int k=0; k<sampleChars.length;k++)  //processing each char
                    {
                        
                         if (sampleChars[k]==' ')
                            {
                                storePos=k;  // this keeps a track of the previous blank space
                            }
                        
                        //block segments are lengthLine;
                        if ((lengthLine % (k+1))== 0)   //this tests if k has reached the last position in the segment
                        // 1 is added to k since it is zero index
                        //there should be no remainder
                        
                        {
                            if (sampleChars[k]==' ')
                            {
                                //this is acceptable for last character in segment since it means that a new word will start in new segment
                                System.out.println(output[segment]);
                                segment++;
                                break;
                            }
                            
                            if (sampleChars[k]!=' '  &&  sampleChars[k+1]==' ' )
                            {
                                //this is acceptable since it means that segment will end with char and new segment start with space
                                System.out.println(output[segment]);
                                segment++;
                                break;
                            }
                            
                            
                            
                            // this is slightly trickier, since the word should not be truncated
                            // AT NO POINT SO FAR HAS IT WRITTEN ANY CHARACTERS
                            
                            if (sample.charAt(k)!=' '  &&  sample.charAt(k+1)!=' ' )
                            {
                                //new string will have to start one position after storePos
                                // and all characters written in current segment from ' ' onwards will need to be wiped
                                // and k the counter will also need to be rolled backwards!
                                
                                //can not replace character like this
                                //sample.charAt(m)=' ';  // blank value written to all values
                                
                                
                                for (int m=storePos+1; m<lengthLine;m++)
                                {
                                    //charsToRewrite++;
                                    
                                    // now need to convert the modified character array back into String
                                    //sample = String.valueOf(charArray);
                                    
                                    System.out.println(output[segment]);
                                    segment++;
                                    
                                    // k the counter can be reset as such
                                    //k=k-charsToRewrite;
                                    
                                    //preferred route of resetting k is as follows
                                    k=storePos+1;
                                    continue;
                                }
                            
                            }
                            
                        }
                    }
            }
        return null;
        
        }
    
   
    
}


// no longer used

/*
for (int i=0; i<sample.length(); i=i+(lengthLine-1))
                {
                    
                    System.out.println("\nvalue of i: " + i);
                    System.out.println("current array is:" + array);
                    
                    // this bit is needed since it will otherwise skip the first character in the nested loop
                    if (array==0)
                    {
                        init=0;
                    }
                    
                    else
                    {
                        init=1;
                    }
                    
                    // it needs to check also that there is a space on last position of each segment or there is no space
                    //at start of next segment
                    
                    
                    for (int k=0; k<lengthLine; k++)
                    {
                        System.out.println("value of K: " + sample.charAt(i+k));
                        
                        //if (sample.charAt(i+k)==' ')
                        //{
                          //  k++;
                    //    }
                        
                        // this requires trickier part of code...
                        // need something to ensure that a word is not truncated.
                        // this means that last character can be a space
                        // or the character after last can be a space
                        // but neither 
                        
                        // logic needs to keep length of each word and then create 
                        
                        if (k=lengthLine-1)
                        {
                            if (   (sample.charAt(i+k)==' ' && sample.charAt(i+k)!=' ')  || (sample.charAt(i+k)!=' ' && sample.charAt(i+k)==' ')  )                     )
                            {
                                System.out.println("correct break up of segments");
                            }
                            
                        }
                        
                        
                        output[array]=output[array] + sample.charAt(i+k);
                        
                        
                    
                     }
                    
                    array++;
                }
                
                
                
                
                 //this is perhaps wrong since it will truncate words....
                stringsRequired=Math.ceil(sample.length()/lengthLine);
                int stringsRequiredConverted = (int) stringsRequired;
                
                //
                System.out.println("length of sample: " + sample.length());
                //System.out.println("number strings required: " + stringsRequired);
                System.out.println("length of strings: " + lengthLine);
                
                
                //
                
                // for now it is setting array of 50 strings, but a better measure will be used soon
                
                */