Kth space | C ++ program of the above approach ; Function to extract integer at key position in the given string ; strtok ( ) : Extracts the number at key c_str ( ) : Type cast string to char * ; Driver Code ; Given string ; Given K ; Function call
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void print_kth_string ( string str , int K ) { char * s = strtok ( ( char * ) str . c_str ( ) , " ▁ " ) ; while ( K > 1 ) { s = strtok ( NULL , " ▁ " ) ; K -- ; } cout << string ( s ) << " ▁ " ; } int main ( ) { string s ( "10 ▁ 20 ▁ 30 ▁ 40" ) ; int K = 2 ; print_kth_string ( s , K ) ; return 0 ; }