Home>

I would like to implement a process of cutting out a numeric value from a character string that contains both alphabetic and numeric values ​​and storing the numeric value in an array, but no solution has been found.
Up to 4 alphabets appearing in the string
Appears in one letter
It becomes.
[A100B20C3000D40]
From the string
[100]
[20]
[3000]
[40]
The purpose is to extract and store in a separate variable.

Tell us what you can think of.
We are sorry for the lack of self-solving and no code.

c
  • Answer # 1

    # include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    int main () {
        char str [] = "A100B20C3000D40";
        char out [100];
        int istr = 0;
        int iout = 0;
        printf ("<<sample1>>\ n");
        for (istr = 0;istr<= strlen (str);++ istr) {
            if (isdigit (str [istr])) {
                out [iout] = str [istr];
                iout ++;
                out [iout] = '\ 0';
            } else if (iout) {
                printf ("% s \ n", out);
                iout = 0;
            }
        }
        printf ("<<sample2>>\ n");
        char * p, * q;
        p = str;
        while (q = strtok (p, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
            p = NULL;
            printf ("% s \ n", q);
        }
    }


    In sample 2, note that the functionstrtokrewrites the contents of the originalstr.

  • Answer # 2

    This can be thought of as a regular expression problem.
    Therefore, there are a method of using a regular expression library and a method of implementing a simplified version thereof.
    Here we consider a method of checking the target character string from the beginning.

    Assume that the target character string is stored in the pointer str.
    The position where str is checked from the beginning is secured with p.
    Then, for example, you can write the following using a for statement.

    # define MAX_NUM 5 // max 5 if the first character is not alphabetic
    const char * str = "A100B20C3000D40";//
    unsigned long long result [MAX_NUM];// answer storage location
    char i = 0;// Storage location
    unsigned long long work = 0;
    // check string str from the beginning
    for (char * p = (char *) str;p! = ¥ 0;p ++)
    {
    ...
    ...
    }

    Next, let's consider two patterns of processing.
    If the current character position is a number, if it is a number, calculate work.
    If it is not a number, that is, if it is an alphabet or the end of a string, the previous number is stored in the result.

    If it is a number,
    Since WORK is the number of the current position, multiply it by 10 and add the current position.

    If not a number
    Store the result of WORK, prepare the next storage location, and clear WORK.

    if (is_number_char (* p)) {
      work = work * 10 + (* p-'0');
    } else {
      result [i] = work;//
      i ++;
      work = 0;
    }
    } // for loop ends
    result [i] = work;// Results after the last alphabet

    where is_number_char is a function that determines whether it is a number.

    int is_number_char (c) {
       return (('0'<= c)&&(c<= '9'))
    }

  • Answer # 3

    There's a strtol function