Kth space | Java program for the above approach ; Function to print kth integer in a given string ; Size of the string ; Pointer for iteration ; If space char found decrement K ; If K becomes 1 , the next string is the required one ; Print the required number ; Driver Code ; Given string ; Given K ; Function call
import java . io . * ; class GFG { static void print_kth_string ( String s , int K ) { int N = s . length ( ) ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ▁ ' ) K -- ; if ( K == 1 ) break ; } while ( i ++ < N - 1 && s . charAt ( i ) != ' ▁ ' ) System . out . print ( s . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String s = "10 ▁ 20 ▁ 30 ▁ 40" ; int K = 4 ; print_kth_string ( s , K ) ; } }