Given a string containing alphanumeric characters, calculate sum of all numbers present in the string.
Input: 1abc23 Output: 24 Input: geeks4geeks Output: 4 Input: 1abc2x30yz67 Output: 100 Input: 123abc Output: 123
Difficulty level: Rookie
The only tricky part in this question is that multiple consecutive digits are considered as one number.
The idea is very simple. We scan each character of the input string and if a number is formed by consecutive characters of the string, we increment the result by that amount.
Below is its implementation –
Time complexity of above solution is O(n) where n is length of the string.
A Better Solution implementing Regex.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Calculate maximum value using '+' or '*' sign between two numbers in a string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the character in first string that is present at minimum index in second string
- Remove characters from the first string which are present in the second string
- Most frequent word in first String which is not present in second String
- Check if given words are present in a string
- Number of substrings of one string present in other
- Count words present in a string
- Python | Check if a Substring is Present in a Given String
- Find repeated character present first in a string
- Check whether the Average Character of the String is present or not
- Find the largest Alphabetic character present in the string
- Program to print characters present at prime indexes in a given string
- Calculate the frequency of each word in the given string
- Calculate score for the given binary string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Check if the given string of words can be formed from words present in the dictionary
- Count of anagrams of each string in an array present in another array
- First string from the given array whose reverse is also present in the same array