How to extract numbers from string in c?

后端 未结 7 817
终归单人心
终归单人心 2020-11-29 04:55

Say I have a string like ab234cid*(s349*(20kd and I want to extract all the numbers 234, 349, 20, what should I do ?

相关标签:
7条回答
  • 2020-11-29 05:12

    If the numbers are seprated by whitespace in the string then you can use sscanf(). Since, it's not the case with your example, you have to do it yourself:

    char tmp[256];
    
    for(i=0;str[i];i++)
    {
      j=0;
      while(str[i]>='0' && str[i]<='9')
      {
         tmp[j]=str[i];
         i++;
         j++;
      }
      tmp[j]=0;
      printf("%ld", strtol(tmp, &tmp, 10));
      // Or store in an integer array
    

    }

    0 讨论(0)
  • 2020-11-29 05:14

    A possible solution using sscanf() and scan sets:

    const char* s = "ab234cid*(s349*(20kd";
    int i1, i2, i3;
    if (3 == sscanf(s,
                    "%*[^0123456789]%d%*[^0123456789]%d%*[^0123456789]%d",
                    &i1,
                    &i2,
                    &i3))
    {
        printf("%d %d %d\n", i1, i2, i3);
    }
    

    where %*[^0123456789] means ignore input until a digit is found. See demo at http://ideone.com/2hB4UW .

    Or, if the number of numbers is unknown you can use %n specifier to record the last position read in the buffer:

    const char* s = "ab234cid*(s349*(20kd";
    int total_n = 0;
    int n;
    int i;
    while (1 == sscanf(s + total_n, "%*[^0123456789]%d%n", &i, &n))
    {
        total_n += n;
        printf("%d\n", i);
    }
    
    0 讨论(0)
  • 2020-11-29 05:16

    Make a state machine that operates on one basic principle: is the current character a number.

    • When transitioning from non-digit to digit, you initialize your current_number := number.
    • when transitioning from digit to digit, you "shift" the new digit in:
      current_number := current_number * 10 + number;
    • when transitioning from digit to non-digit, you output the current_number
    • when from non-digit to non-digit, you do nothing.

    Optimizations are possible.

    0 讨论(0)
  • 2020-11-29 05:22

    You can do it with strtol, like this:

    char *str = "ab234cid*(s349*(20kd", *p = str;
    while (*p) { // While there are more characters to process...
        if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
            // Found a number
            long val = strtol(p, &p, 10); // Read number
            printf("%ld\n", val); // and print it.
        } else {
            // Otherwise, move on to the next character.
            p++;
        }
    }
    

    Link to ideone.

    0 讨论(0)
  • 2020-11-29 05:29

    here after a simple solution using sscanf:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char str[256]="ab234cid*(s349*(20kd";
    char tmp[256];
    
    int main()
    {
    
        int x;
        tmp[0]='\0';
        while (sscanf(str,"%[^0123456789]%s",tmp,str)>1||sscanf(str,"%d%s",&x,str))
        {
            if (tmp[0]=='\0')
            {
                printf("%d\r\n",x);
            }
            tmp[0]='\0';
    
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:29
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    void main(int argc,char *argv[])
    {
    char *str ="ab234cid*(s349*(20kd", *ptr = str;
    while (*ptr) { // While there are more characters to process...
        if ( isdigit(*ptr) ) {
            // Found a number
            int val = (int)strtol(ptr,&ptr, 10); // Read number
            printf("%d\n", val); // and print it.
        } else {
            // Otherwise, move on to the next character.
            ptr++;
        }
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题