![]() |
VOOZH | about |
Given a string s consisting of lowercase letters, uppercase letters, digits, and special characters, extract all the integers present in the string and return them in the order they appear.
If no integers are present in the string, return an empty array.
Example:
Input: s = "1: Geeks for geeks, 2: geeksfor geeks, 3: forGeeksgeeks 56"
Output: [1, 2, 3, 56]
Explanation: 1, 2, 3, 56 are the integers present in s.Input: s = "geeksforgeeks"
Output: []
Explanation: No integers present in the string.
Table of Content
The idea is to traverse the string character by character and collect consecutive digit characters to form integers. A temporary string is used to store digits while traversing. Whenever a non-digit character is encountered, the collected number is stored in the answer vector and the temporary string is cleared. After the traversal ends, any remaining number is also added to the result.
500 100
The idea is to use regular expressions to directly find all continuous sequences of digits present in the string. The regex pattern
\\d+matches one or more consecutive digit characters.
\\d+ → matches continuous digits regex_search() to find integers in the string 500 100