Separate string and numbers (Ex.: 00oy285dase556 - num1=00, num2=285, num3=556)

Asked

Viewed 21 times

-1

I need to create a reading module, the variable rate, of numerical values in the port Serial, receiving an integer string, interspersed with letters and using the letters as separator to obtain only the numbers in C. (Ex.: 00oy285dase556 - num1=00, num2=285, num3=556)

  • Define your problem better. Is the number undefined? Should only be considered integers or can there be real numbers? Is it allowed to use dynamic memory allocation? Post the code you have already tried. Read Manual on how NOT to ask questions

1 answer

0

You can start from something like:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
    int i=0, j=0, k=0, l, num[10];
    char buffer[256], aux[20];
    printf ("Informe a entrada: ");
    fgets (buffer, 255, stdin);
    while (buffer[i] != '\0') {
        while (buffer[i] != '\0' && !isdigit(buffer[i]))
            i++;
        if (buffer[i] != '\0') {
            j = 0;
            while (buffer[i] != '\0' && isdigit(buffer[i]))
                aux[j++] = buffer[i++];
            aux[j] = '\0';
            num[k++] = atoi (aux);
        }
    }
    for (l=0; l<k; l++)
        printf ("numero[%d] = %d\n",l, num[l]);
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.