c - Need to take multiple digit input from a string removing spaces and alphabets -
i writing program taking string input, here need remove spaces , ignore alphabets , use numerals.
i able achieve removing spaces , alphabets, can use single digits , not multiple digits.
example:input string:"adsf 12af 1 a123c 53c2m34n"
here need use input "12 1 123 54234" required application.
it great 1 share logic or sample code same.
thanks in advance
#include <stdio.h> #include <string.h> #include <ctype.h> int pullout(const char *str, int array[], int *size){ const char *p = str, *endp; int pull, count = 0, max = *size, num; do{ endp=strchr(p, ' '); if(endp == null) endp=strchr(p, '\0'); for(num=pull=0; p != endp; ++p){ if(isdigit(*p)){ num = num * 10 + *p - '0'; pull = 1; } } if(pull && count < max) array[count++] = num; while(*p == ' ') ++p;//skip sapce }while(*endp != '\0'); return *size = count; } int main(void){ char input[] = "adsf 12af 1 a123c 53c2m34n abc def"; int i, arr[128] = { 0 }, arr_num = sizeof(arr)/sizeof(int); pullout(input, arr, &arr_num); for(i = 0; < arr_num ; ++i) printf("%d\n", arr[i]); return 0; }
Comments
Post a Comment