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 the implementation of the above idea:
Time complexity: 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 firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Remove characters from the first string which are present in the second 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
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Calculate maximum value using '+' or '*' sign between two numbers in a string
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Count of words that are present in all the given sentences
- Java program to read all Emails present in a Given file
- Count words present in a string
- Find repeated character present first in a string
- Check if the given string of words can be formed from words present in the dictionary
- Python | Check if a Substring is Present in a Given String
- Program to print characters present at prime indexes in a given string
- Find the largest Alphabetic character present in the string
- Number of substrings of one string present in other
- Check whether the Average Character of the String is present or not
- First string from the given array whose reverse is also present in the same array
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Check if given words are present in a string