## Find a sum of numbers in a given string

sum of numbers in a string javascript

to find sum of all the numbers in a string in javascript

how to add numbers in a string python

sum of digits in a string c++

sum of the alphabetical values of the characters of a string

sum of embedded numbers in string in c

how to find digit in string in java

I have to find the sum of the numbers in a given string For example for "a11b3" the amount will be refunded 14 because (11+3=14) I wrote the following code but it does not work for me in any situation, how can I optimize it?

int SumStr(char* str) { int i, sum = 0, digit = 0; while (*str) { digit = 0; while (*str >= '0' && *str <= '9' && *str) { digit = digit * 10 + (*str - '0'); str++; } if (digit > 0) sum += digit; str++; } return sum; }

You can loop through the string with a pointer; when a digit is found the pointer can be passed to `strtol()`

to convert the remaining part of the string to a number. The `strtol()`

function accepts a pointer to the initial part of the string to be converted (a `char *`

), and a pointer to a pointer used to store a pointer to the remaining portion of the string that could not be converted (a `char **`

). This end pointer can be given the address of `str`

in this case, so that after a number is converted, the search for digits resumes after the end of the converted number.

Note that `strtol()`

returns a `long`

value. The `sum_str()`

function below returns a `long`

. You could return an `int`

instead if desired, yet if the final `sum`

value were too large to fit into an `int`

the value returned would be implementation dependent.

Some tests to detect overflow could be added in the summation loop (and this would be a good idea). No provision has been made (and there is no requirement provided) for negative numbers in the input string. Note that a simple change to allow for negative numbers in the input string would just involve testing for a `'-'`

character followed by a digit:

if (isdigit(*str) || (*str == '-' && isdigit(*(str + 1)))) { sum += strtol(str, &str, 10);

The input string should probably be validated before giving it to the function. Should strings with no digits be allowed? Should strings with more than one character between numbers be allowed? Should strings with a terminal non-digit be allowed? Should strings with decimal points be allowed? The answers to these questions would allow the `sum_str()`

function and the code that calls it to be refined. In the code below strings containing no digits return a sum of zero, groups of one or more non-digit characters, including decimal points, serve to separate the digits to be summed, and terminal characters are ignored after the final digit.

#include <stdio.h> #include <stdlib.h> #include <ctype.h> long sum_str(char *str); int main(void) { printf("%ld\n", sum_str("a11b3c-4")); return 0; } long sum_str(char *str) { long sum = 0; while(*str) { if (isdigit(*str)) { sum += strtol(str, &str, 10); } if (*str) { // don't increment if end of string reached ++str; } } return sum; }

Program output:

14

**How to calculate sum of all numbers in a string,** Parsing chars back to String and then to Integer is too expensive, since you already have a char. You should try doing this: String a = "jklmn489pjro635ops"; int Given a string of alpha numeric characters, write a function that will calculate all the numbers present in that string Example. INPUT s = “12bc1nk8” OUTPUT 21 In the above example the sum is 12+1+8 = 21. Algorithm. 1. Traverse through the given string. 2. If a number is formed with the consecutive characters, then add that to the previous sum. C++ Program

*How can I optimize it?*. I am not sure how the following will optimize in terms of speed or memory size, but it does make the code a little simpler, and more readable:

The following uses the `isdigit()`

function and *ASCII values* to count numbers:

int main(void) { char buf[] = {"abc23jrt5"};//sum of integers: 23 + 5 = 28 int count = int SumStr(buf); return 0; } //Edited to sum instances of numbers embedded in string. //adjacent digits such as `11` in the string `avf11e5` are treated //as the integer value `11`, not `1 + 1`. int SumStr(char *buf) { int i = 0; int sum = 0; while( *buf != 0 ) { i = 0; while(isdigit(*buf)) { i *= 10, i += *buf - '0'; if(*buf != 0) buf++; } sum += i; if(*buf != 0) buf++; } return sum; };

**Sum Integers written into a String,** How do you find the sum of a string of numbers? In the regular expression, ‘\d+’ used to find all integer value in the given input from the user. Find the sum of numbers in the given string in Python. As we have seen that the number is in list form that why for summation of all integer we have to run a for loop in the program. Now we are going to write the Python code. Python Code:

You do one more `str++`

than you should.

int SumStr(const char *str) { // added const int sum = 0, digit = 0; // removed i while (*str) { digit = 0; while (*str && *str >= '0' && *str <= '9') { // swapped order of conditions digit = digit * 10 + (*str - '0'); str++; } if (digit > 0) sum += digit; else str++; // added else } return sum; }

**How can I find the sum of two numbers which are in String variables ,** How do you sum numbers in a string in Java? How do I calculate the sum of all the numbers in a string? In the example below, the expected result would be 4+8+9+6+3+5. My attempt is below. Also could I calculate the sum of only those numbers

**Calculate sum of all numbers present in a string,** How do I find all the substrings in a string? String s1 = sc.nextLine(); getvalues(s1); } public static void getvalues(String s1) {. int sum = 0; for (int i = 0; i < s1.length(); i++) {. char a = s1.charAt(i); if (Character.isDigit(a)) {. int b = Integer.parseInt(String.valueOf(a));

**Java exercises: Return the sum of the digits present in the given string,** Given a string of alpha numeric characters, write a function that will calculate all the numbers present in that string. INPUT. s = “12bc1nk8” OUTPUT. In the above example the sum is 12+1+8 = 21. Traverse through the given string. If a number is formed with the consecutive characters, then add that to the previous sum. Java String: Exercise-95 with Solution. Write a Java program to return the sum of the digits present in the given string. If there is no digits the sum return is 0.

**Python: Compute sum of digits of a given string,** If there is no digits the sum return is 0. The given string is: ab5c2d4ef12s The sum of the digits in the string is: 14 Previous: Write a Java program to find the longest mirror image string at the both ends of a given string. Kotlin program to find the sum of all numbers in an array. One number array is given and we need to find out the sum of its numbers. Method 1: Using sum() : Array in kotlin provides one method called sum that returns the sum of all elements of an array. Just call this method sum() to an array :

##### Comments

- Use the
`isdigit()`

C function to test each`char`

as you loop through the string. - It does not work or does work? It works with the example you have given.
- You can use the
`strtol()`

function in`<stdlib.h>`

. - What is
`sum += digit`

? What you expect? Imagine`digit`

is 129. But this is 3 characters, not 129. BTW, digit and number are different things. - yes the str++ in the end were the problem tnx all!
- you need to change the name of the function countNumbers to SumStr
- Its returns sum of 10 i need it return 23+5=28
- @Eden - Once that became clear in the comments, it would be useful that you add that specification into your post. The answer is edited.