Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

10,405 questions

14,169 answers

573 users

How to sum all numbers from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
 
int sum_numbers(char s[]) { 
    int sum = 0, num = 0;
     
    char *p = s;
    while (*p) {
        if (isdigit(*p)) {
            num = num * 10 + strtol(p, &p, 10);
        } 
        else {
            p++;
            sum += num;
            num = 0;
        }
    }
    return sum + num;
} 
   
int main() 
{ 
    char s[] = "1c32 java 60"; 

    printf("%i", sum_numbers(s)); 
   
    return 0; 
} 
 
 
/*
run:
 
93
 
*/

 




answered May 27 by avibootz
edited May 27 by avibootz
...