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.
- 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
- Find the character in first string that is present at minimum index in second string
- Most frequent word in first String which is not present in second String
- Remove characters from the first string which are present in the second string
- Count words present in a string
- Number of substrings of one string present in other
- Find repeated character present first in a string
- Python | Check if a Substring is Present in a Given String
- Check whether the Average Character of the String is present or not
- Program to print characters present at prime indexes in a given string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Calculate score for the given binary string
- Check if the given string of words can be formed from words present in the dictionary
- First string from the given array whose reverse is also present in the same array