Splitting a Numeric String | Java program to split a numeric string in an Increasing sequence if possible ; Function accepts a string and checks if string can be split . ; if there is only 1 number in the string then it is not possible to split it ; storing the substring from 0 to i + 1 to form initial number of the increasing sequence ; convert string to integer and add 1 and again convert back to string s2 ; if s2 is not a substring of number than not possible ; if s2 is the next substring of the numeric string ; Incearse num2 by 1 i . e the next number to be looked for ; check if string is fully traversed then break ; If next string doesnot occurs in a given numeric string then it is not possible ; if the string was fully traversed and conditions were satisfied ; if conditions failed to hold ; Driver Code ; Call the split function for splitting the string
import java . io . * ; import java . util . * ; class GFG { public static void split ( String str ) { int len = str . length ( ) ; if ( len == 1 ) { System . out . println ( " Not ▁ Possible " ) ; return ; } String s1 = " " , s2 = " " ; long num1 , num2 ; for ( int i = 0 ; i <= len / 2 ; i ++ ) { int flag = 0 ; s1 = str . substring ( 0 , i + 1 ) ; num1 = Long . parseLong ( ( s1 ) ) ; num2 = num1 + 1 ; s2 = Long . toString ( num2 ) ; int k = i + 1 ; while ( flag == 0 ) { int l = s2 . length ( ) ; if ( k + l > len ) { flag = 1 ; break ; } if ( ( str . substring ( k , k + l ) . equals ( s2 ) ) ) { flag = 0 ; num2 ++ ; k = k + l ; if ( k == len ) break ; s2 = Long . toString ( num2 ) ; l = s2 . length ( ) ; if ( k + 1 > len ) { flag = 1 ; break ; } } else flag = 1 ; } if ( flag == 0 ) { System . out . println ( " Possible " + " ▁ " + s1 ) ; break ; } else if ( flag == 1 && i > len / 2 - 1 ) { System . out . println ( " Not ▁ Possible " ) ; break ; } } } public static void main ( String args [ ] ) { Scanner in = new Scanner ( System . in ) ; String str = "99100" ; split ( str ) ; } }