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 iThemes WordPress Hosting

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

DreamHost Web Hosting


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

Liquid Web Cloud VPS Hosting

10,903 questions

14,810 answers

573 users

How to extract all integers (numbers) from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
  
void extract_numbers(char s[]) { 
    int num = 0;
      
    char *p = s;
    while (*p) {
        if (isdigit(*p)) {
            num = num * 10 + strtol(p, &p, 10);
        } 
        else {
            p++;
            if (num != 0) printf("%i\n", num);
            num = 0;
        }
    }
} 
    
int main() 
{ 
    char s[] = "1c32 ja60val"; 

    extract_numbers(s); 
    
    return 0; 
} 
  
  
/*
run:
  
1
32
60
  
*/

 




answered May 27 by avibootz
...