📜  从给定的字符串中提取所有整数

📅  最后修改于: 2022-05-13 01:57:06.549000             🧑  作者: Mango

从给定的字符串中提取所有整数

给定一个字符串str ,从中提取所有整数单词。

例子:

方法:要解决此问题,请按照以下步骤操作:

  1. 创建一个字符串untilNow ,它将存储所有已建立的整数到现在。用空字符串初始化它。
  2. 现在开始遍历字符串str ,并且在每次迭代中:
    1. 如果字符是数字,则将其添加到字符串untilNow
    2. 否则,检查字符串untilNow是否为空。如果不为空,则将其转换为整数并在打印后将其清空。
  3. 现在,在循环结束后,再次检查字符串untilNow是否为空。如果不是,则将其转换为整数并打印。

下面是上述方法的实现

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to extract integers from
// the string str
void extractIntegers(string str)
{
    int n = str.size();
 
    // This variable will store each founded
    // integer temporarily
    string tillNow = "";
 
    for (int i = 0; i < n; i++) {
 
        // If current character is an integer, then
        // add it to string tillNow
        if (str[i] - '0' >= 0 and str[i] - '0' <= 9) {
            tillNow += str[i];
        }
 
        // Otherwise, check if tillNow is empty or not
        // If it isn't then convert tillNow to integer
        // and empty it after printing
        else {
            if (tillNow.size() > 0) {
                cout << stoi(tillNow) << ' ';
 
                tillNow = "";
            }
        }
    }
 
    // if tillNow isn't empty then convert tillNow
    // to integer and print it
    if (tillNow.size() > 0) {
        cout << stoi(tillNow) << ' ';
    }
}
 
// Driver Code
int main()
{
    string str = "Hey everyone, "
                 "I have 500 rupees and"
                 " I would spend a 100";
    extractIntegers(str);
}


Java
// Java program for the above approach
class GFG{
 
// Function to extract integers from
// the String str
static void extractIntegers(char []str)
{
    int n = str.length;
 
    // This variable will store each founded
    // integer temporarily
    String tillNow = "";
 
    for (int i = 0; i < n; i++) {
 
        // If current character is an integer, then
        // add it to String tillNow
        if (str[i] - '0' >= 0 && str[i] - '0' <= 9) {
            tillNow += str[i];
        }
 
        // Otherwise, check if tillNow is empty or not
        // If it isn't then convert tillNow to integer
        // and empty it after printing
        else {
            if (tillNow.length() > 0) {
                System.out.print(Integer.parseInt(tillNow) +" ");
 
                tillNow = "";
            }
        }
    }
 
    // if tillNow isn't empty then convert tillNow
    // to integer and print it
    if (tillNow.length() > 0) {
        System.out.print(Integer.parseInt(tillNow) +" ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "Hey everyone, "
                 +"I have 500 rupees and"
                 +" I would spend a 100";
    extractIntegers(str.toCharArray());
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to extract integers from
# the string str
def extractIntegers(string) :
 
    n = len(string);
 
    # This variable will store each founded
    # integer temporarily
    tillNow = "";
 
    for i in range(n) :
 
        # If current character is an integer, then
        # add it to string tillNow
        if (ord(string[i]) - ord('0') >= 0 and ord(string[i]) - ord('0') <= 9) :
            tillNow += string[i];
 
        # Otherwise, check if tillNow is empty or not
        # If it isn't then convert tillNow to integer
        # and empty it after printing
        else :
            if (len(tillNow) > 0) :
                print(int(tillNow),end = ' ');
 
                tillNow = "";
 
    # if tillNow isn't empty then convert tillNow
    # to integer and print it
    if (len(tillNow) > 0) :
        print(int(tillNow),end = ' ');
 
 
# Driver Code
if __name__ == "__main__" :
 
    string  = "Hey everyone, I have 500 rupees and I would spend a 100";
     
    extractIntegers(string);
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to extract integers from
    // the String str
    static void extractIntegers(char[] str)
    {
        int n = str.Length;
 
        // This variable will store each founded
        // integer temporarily
        String tillNow = "";
 
        for (int i = 0; i < n; i++)
        {
 
            // If current character is an integer, then
            // add it to String tillNow
            if (str[i] - '0' >= 0 && str[i] - '0' <= 9)
            {
                tillNow += str[i];
            }
 
            // Otherwise, check if tillNow is empty or not
            // If it isn't then convert tillNow to integer
            // and empty it after printing
            else
            {
                if (tillNow.Length > 0)
                {
                    Console.Write(int.Parse(tillNow) + " ");
 
                    tillNow = "";
                }
            }
        }
 
        // if tillNow isn't empty then convert tillNow
        // to integer and print it
        if (tillNow.Length > 0)
        {
            Console.Write(int.Parse(tillNow) + " ");
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "Hey everyone, "
                     + "I have 500 rupees and"
                     + " I would spend a 100";
        extractIntegers(str.ToCharArray());
    }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript


输出
500 100 

时间复杂度: O(N)
辅助空间: O(N)