Given a string S consisting of space-separated integers, the task is to extract the Kth number present in the string.
Note: The string contains at least K numbers in it.
Examples:
Input: S = “12 13 15”, K= 3
Output: 15
Explanation: The 3rd integer in the above string is 15.
Input: S = “10 20 30 40”, K = 2
Output: 20
Explanation: The 2nd integer in the above string is 20.
Naive Approach: The simplest approach to solve the problem is to traverse the string and keep a count of spaces encountered. Once, K – 1 spaces are encountered, print the number up to the next space as the required answer.
Below is the implementation of the above approach:
C++
#include <iostream>
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);
}
|
Java
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);
}
}
|
Python3
def print_kth_string(s, K):
N = len (s);
for i in range ( 0 , N, 1 ):
if (s[i] = = ' ' ):
K - = 1 ;
if (K = = 1 ):
break ;
while (i < N):
if (s[i] ! = ' ' ):
print (s[i], end = "");
i + = 1 ;
if __name__ = = '__main__' :
s = "10 20 30 40" ;
K = 4 ;
print_kth_string(s, K);
|
C#
using System;
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[i] == ' ' )
K--;
if (K == 1)
break ;
}
while (i++ < N - 1 && s[i] != ' ' )
Console.Write(s[i]);
}
public static void Main ()
{
string s = "10 20 30 40" ;
int K = 4;
print_kth_string(s, K);
}
}
|
Javascript
<script>
function print_kth_string( s , K) {
var N = s.length;
var i;
for (i = 0; i < N; i++) {
if (s.charAt(i) == ' ' )
K--;
if (K == 1)
break ;
}
while (i++ < N - 1 && s.charAt(i) != ' ' )
document.write(s.charAt(i));
}
var s = "10 20 30 40" ;
var K = 4;
print_kth_string(s, K);
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Stringstream Approach: The idea is to use stringstream in C++ which associates a string object with a stream and allowing us to read from the string as if it were a stream (like cin).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print_kth_string(string str, int K)
{
stringstream iss(str);
string kth;
while (iss >> kth && K) {
K--;
if (K == 0) {
cout << kth;
break ;
}
}
}
int main()
{
string s( "10 20 30 40" );
int K = 4;
print_kth_string(s, K);
}
|
Java
import java.util.*;
class GFG{
static void print_kth_String(String str,
int K)
{
String[] iss = str.split( " " );
K--;
System.out.print(iss[K]);
}
public static void main(String[] args)
{
String s = ( "10 20 30 40" );
int K = 4 ;
print_kth_String(s, K);
}
}
|
Python3
def print_kth_string(str1, K):
st = str1.split( " " )
print (st[K - 1 ])
if __name__ = = '__main__' :
s = "10 20 30 40"
K = 4
print_kth_string(s, K)
|
C#
using System;
class GFG{
static void print_kth_String(String str,
int K)
{
String[] iss = str.Split( ' ' );
K--;
Console.Write(iss[K]);
}
public static void Main(String[] args)
{
String s = ( "10 20 30 40" );
int K = 4;
print_kth_String(s, K);
}
}
|
Javascript
function print_kth_String(str, K) {
let iss = str.split( " " );
K--;
console.log(iss[K]);
}
let s = "10 20 30 40" ;
let K = 4;
print_kth_String(s, K);
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Built-in String Functions based Approach: The idea is to use strtok() function to extract the string at the key. Use c_str() function to get the char pointer reference of an array of the characters.
Below is the implementation of the above approach:
C++
#include <cstring>
#include <iostream>
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;
}
|
Java
import java.util.*;
class GFG{
static void print_kth_String(String str,
int K)
{
StringTokenizer st =
new StringTokenizer(str);
int count = 1 ;
while (st.hasMoreTokens() && K > 0 )
{
if (count == K)
System.out.println(st.nextToken());
count++;
st.nextToken();
}
}
public static void main(String[] args)
{
String s = ( "10 20 30 40" );
int K = 2 ;
print_kth_String(s, K);
}
}
|
Python3
import re
def print_kth_string(string, K):
s = re.split( " " , string)
return s[K - 1 ]
if __name__ = = '__main__' :
s = "10 20 30 40"
K = 2
print (print_kth_string(s, K))
|
C#
using System;
class GFG{
static void print_kth_String(String str,
int K)
{
String[] iss = str.Split( ' ' );
K--;
Console.Write(iss[K]);
}
public static void Main(String[] args)
{
String s = ( "10 20 30 40" );
int K = 2;
print_kth_String(s, K);
}
}
|
Javascript
function print_kth_string(str, K) {
let words = str.split( " " );
console.log(words[K-1]);
}
let s = "10 20 30 40" ;
let K = 2;
print_kth_string(s, K);
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)