How to extract numbers from string in c?

后端 未结 7 864
终归单人心
终归单人心 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: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.

提交回复
热议问题