Sum of numbers in String

Difficulty Level Easy
Frequently asked in Adobe Fanatics MAQ
StringViews 6580

In this question, we will learn how to calculate the Sum of numbers in String

Problem Statement

In the “Calculate Sum of all Numbers Present in a String” problem we have given a string “s”. This string contains some alphanumeric numbers and some English lowercase characters. Write a program that will calculate all the numbers present in that string and print the final answer.

Input Format

The first and only one line containing a string “s”.

Output Format

The first and only one line containing an integer value N which represents the sum of all numbers present in the string.

Constraints

  • 1<=|s|<=10^6
  • s[i] must be a lower case English alphabet or a digit from 0 to 9(inclusive).
  • The number present in the given string “s” is not greater than 10^9

Example

a123b12c1d
136

Explanation: Here the number present in the given string “a123b12c1d” are 123, 12, 1. So, the sum of these numbers is 136.

Algorithm

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.

  1. Set “ans” to zero and take input string from the user in “s”.
  2. Traverse the string character by character.
  3. If the current character is a lower case English alphabet then move to the next character.
  4. Else update ans by that amount only.
  5. Print the final answer which is stored in “ans”.

Implementation

C++ Program to Calculate Sum of all Numbers Present in a String

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
  string s;
  cin>>s;
  int ans=0;
  string t="";
    for(char ch : s) 
    { 
    if(ch>='0' && ch<='9') 
    {
        t+=ch;
    }
    else 
    { 
        if(t.length()>0)
      ans+=stoi(t);  
      t=""; 
    } 
  }
  if(t.length()>0)
  ans+=stoi(t);  
  cout<<ans<<endl;
  return 0; 
}

Java Program to find Sum of numbers in String

import java.util.Scanner;
import java.util.Vector;

class sum
{
    public static void main(String[] args)
    {
        Scanner sr = new Scanner(System.in);
        String s = sr.next();
  int ans=0;
  String t="";
        for(int i=0;i<s.length();i++) 
        { 
                    if(s.charAt(i)>='0' && s.charAt(i)<='9') 
                    {
                        t+=s.charAt(i);
                    }
                    else 
                    { 
                        if(t.length()>0)
                            ans+=Integer.parseInt(t);  
                            t=""; 
                    } 
            }
            if(t.length()>0)
            ans+=Integer.parseInt(t);  
            System.out.println(ans);
        }
}




123a21bc1sqvaus
145

Complexity Analysis to calculate Sum of numbers in String

Time Complexity

O(n) where n is the length of the given string “s”. Here we visit the whole string char by char and perform the operation in constant time.

Space Complexity

O(1) because we only store the sum of the number present in the given string.

Translate »