Kth space | C ++ 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
#include <iostream> NEW_LINE using namespace std ; void print_kth_string ( string s , int K ) { int N = s . length ( ) ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ▁ ' ) K -- ; if ( K == 1 ) break ; } while ( i ++ < N && s [ i ] != ' ▁ ' ) cout << s [ i ] ; } int main ( ) { string s ( "10 ▁ 20 ▁ 30 ▁ 40" ) ; int K = 4 ; print_kth_string ( s , K ) ; }