Extract all integers from a given String
Given a string str, extract all integers words from it.
Example:
Input: str = “1Hello2 &* how are y5ou”
Output: 1 2 5
Input: str = “Hey everyone, I have 500 rupees and I would spend a 100”
Output: 500 100
Approach: To solve this problem follow the below steps:
- Create a string tillNow, which will store all founded integers till now. Initialise it with an empty string.
- Now start traversing string str, and in each iteration:
- If a character is a number, add it to the string tillNow.
- Otherwise, check if the string tillNow is empty or not. If it isn’t empty, then convert it to an integer and empty it after printing it.
- Now, after the loop ends, check again that if the string tillNow is empty or not. If it isn’t then, convert it to integer and print it.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
void extractIntegers(string str)
{
int n = str.size();
string tillNow = "" ;
for ( int i = 0; i < n; i++) {
if (str[i] - '0' >= 0 and str[i] - '0' <= 9) {
tillNow += str[i];
}
else {
if (tillNow.size() > 0) {
cout << stoi(tillNow) << ' ' ;
tillNow = "" ;
}
}
}
if (tillNow.size() > 0) {
cout << stoi(tillNow) << ' ' ;
}
}
int main()
{
string str = "Hey everyone, "
"I have 500 rupees and"
" I would spend a 100" ;
extractIntegers(str);
}
|
Java
class GFG{
static void extractIntegers( char []str)
{
int n = str.length;
String tillNow = "" ;
for ( int i = 0 ; i < n; i++) {
if (str[i] - '0' >= 0 && str[i] - '0' <= 9 ) {
tillNow += str[i];
}
else {
if (tillNow.length() > 0 ) {
System.out.print(Integer.parseInt(tillNow) + " " );
tillNow = "" ;
}
}
}
if (tillNow.length() > 0 ) {
System.out.print(Integer.parseInt(tillNow) + " " );
}
}
public static void main(String[] args)
{
String str = "Hey everyone, "
+ "I have 500 rupees and"
+ " I would spend a 100" ;
extractIntegers(str.toCharArray());
}
}
|
Python3
def extractIntegers(string) :
n = len (string);
tillNow = "";
for i in range (n) :
if ( ord (string[i]) - ord ( '0' ) > = 0 and ord (string[i]) - ord ( '0' ) < = 9 ) :
tillNow + = string[i];
else :
if ( len (tillNow) > 0 ) :
print ( int (tillNow),end = ' ' );
tillNow = "";
if ( len (tillNow) > 0 ) :
print ( int (tillNow),end = ' ' );
if __name__ = = "__main__" :
string = "Hey everyone, I have 500 rupees and I would spend a 100" ;
extractIntegers(string);
|
C#
using System;
class GFG
{
static void extractIntegers( char [] str)
{
int n = str.Length;
String tillNow = "" ;
for ( int i = 0; i < n; i++)
{
if (str[i] - '0' >= 0 && str[i] - '0' <= 9)
{
tillNow += str[i];
}
else
{
if (tillNow.Length > 0)
{
Console.Write( int .Parse(tillNow) + " " );
tillNow = "" ;
}
}
}
if (tillNow.Length > 0)
{
Console.Write( int .Parse(tillNow) + " " );
}
}
public static void Main(String[] args)
{
String str = "Hey everyone, "
+ "I have 500 rupees and"
+ " I would spend a 100" ;
extractIntegers(str.ToCharArray());
}
}
|
Javascript
<script>
function extractIntegers(str) {
let n = str.length;
let tillNow = "" ;
for (let i = 0; i < n; i++) {
if (str[i].charCodeAt(0) - '0' .charCodeAt(0) >= 0 && str[i].charCodeAt(0) - '0' .charCodeAt(0) <= 9) {
tillNow += str[i];
}
else {
if (tillNow.length > 0) {
document.write(tillNow + ' ');
tillNow = "";
}
}
}
// if tillNow isn' t empty then convert tillNow
if (tillNow.length > 0) {
document.write(tillNow + ' ' );
}
}
let str = "Hey everyone, I have 500 rupees and I would spend a 100" ;
extractIntegers(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2: Using Regex
C++
#include <bits/stdc++.h>
using namespace std;
void extractIntegers(string sentence)
{
stringstream ss(sentence);
string temp;
vector< int > integers;
while (ss >> temp) {
if (temp[0] >= '0' && temp[0] <= '9' ) {
integers.push_back(stoi(temp));
}
}
for ( int i : integers)
cout << i << " " ;
}
int main()
{
string sentence1 = "Hey everyone I have 500 rupees and "
"I would spend a 100" ;
extractIntegers(sentence1);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void extractIntegers(String sentence)
{
String splittedWords[] = sentence.split(
"[^0-9]+" );
for (String i : splittedWords)
System.out.print(i + " " );
}
public static void main(String[] args)
{
String sentence1
= "Hey everyone I have 500 rupees and I would spend a 100" ;
extractIntegers(sentence1);
}
}
|
Python3
import re
def extract_integers(sentence):
splitted_words = re.findall(r '\b\d+\b' , sentence)
for i in splitted_words:
print (i, end = " " )
sentence1 = "Hey everyone I have 500 rupees and I would spend a 100"
extract_integers(sentence1)
|
C#
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class GFG {
static void extractIntegers( string sentence)
{
string [] splittedWords
= Regex.Split(sentence, "[^0-9]+" );
foreach ( string i in splittedWords) {
if (! string .IsNullOrEmpty(i)) {
Console.Write(i + " " );
}
}
}
static public void Main()
{
string sentence1
= "Hey everyone I have 500 rupees and I would spend a 100" ;
extractIntegers(sentence1);
}
}
|
Javascript
function extractIntegers(sentence)
{
let splittedWords = sentence.match(/\b\d+\b/g);
for (let i = 0; i < splittedWords.length; i++) {
console.log(splittedWords[i], " " );
}
}
let sentence1 = "Hey everyone I have 500 rupees and I would spend a 100" ;
extractIntegers(sentence1);
|
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...