Calculate sum of all numbers present in a string | Function to calculate sum of all numbers present in a str1ing containing alphanumeric characters ; A temporary str1ing ; holds sum of all numbers present in the str1ing ; read each character in input string ; if current character is a digit ; if current character is an alphabet ; increment Sum by number found earlier ( if any ) ; reset temporary str1ing to empty ; atoi ( temp . c_str1 ( ) ) takes care of trailing numbers ; input alphanumeric str1ing ; Function call
def findSum ( str1 ) : NEW_LINE INDENT temp = "0" NEW_LINE Sum = 0 NEW_LINE for ch in str1 : NEW_LINE INDENT if ( ch . isdigit ( ) ) : NEW_LINE INDENT temp += ch NEW_LINE DEDENT else : NEW_LINE INDENT Sum += int ( temp ) NEW_LINE temp = "0" NEW_LINE DEDENT DEDENT return Sum + int ( temp ) NEW_LINE DEDENT str1 = "12abc20yz68" NEW_LINE print ( findSum ( str1 ) ) NEW_LINE