sum of digits in a string

Solutions on MaxInterview for sum of digits in a string by the best coders in the world

showing results for - "sum of digits in a string"
Anthea
21 Jan 2019
1# Here, first a complex input is used to determine the sum of all the integers in that input.
2
3# Rules : 1
4
5s = input()
6c = ""
7for i in s:
8    if i.isdigit():
9        c += i
10sum_d = sum(int(j) for j in c)
11print(sum_d)
12
13# Rules : 2
14
15s = input()
16c = ""
17sum = 0
18for i in s:
19    if i.isdigit():
20        c += i
21for j in c:
22  sum += int(c)%10
23  c = int(c)//10
24print(sum)
25
26# Sample input: murad#123#$%&*$%!@*34
27# Sample output: 13
28
29
30# Eval method using to problem:
31# Problem name: Python Evaluation
32# Code
33string = eval(input(""))
34print()
35
36# Sample Input: print(2 + 3)
37# Sample Output: 5
38
39
Amanda
18 Sep 2020
1String -- sum of digits in a string
2Write a method that can return the sum of the digits in a string
3 
4
5
6public  static int  sumOfDigits(String s) {
7  int total = 0;
8char[] ch =  s.toCharArray();
9for(char each: ch) {
10if(Character.isDigit(each)) {
11total += Integer.valueOf(""+each);
12}
13}
14return total;
15}