How to print the numbers in string with C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

void remove_all_spaces(char *s) {
    const char *p = s;
    do {
        while (*p == ' ') {
            p++;
        }
    } while ((*s++ = *p++));
}  
  
void print_numbers(char s[]) { 
	char *tmp = strdup(s);
	remove_all_spaces(tmp);
    int num = 0;
      
    char *p = tmp;
    while (*p) {
        if (isdigit(*p)) {
            num = num * 10 + strtol(p, &p, 10);
        } 
        else {
            p++;
            printf("%d\n", num);
            num = 0;
        }
    }
	free(tmp);
} 
    
int main() 
{ 
	char s[] = "1, 6472, 7, 9, 12, 899";

	print_numbers(s);
    
    return 0; 
} 
  


     
/*
run:
      
1
6472
7
9
12
 
*/

 



answered Jul 2, 2020 by avibootz

Related questions

1 answer 178 views
1 answer 108 views
2 answers 126 views
2 answers 157 views
2 answers 212 views
212 views asked Sep 25, 2021 by avibootz
2 answers 201 views
...