Extracting numbers from a string in C + +

Source: Internet
Author: User

With standard input and output:

Input: 12&3 34*133^3131 13031*

Output: 12 3 34 133 3131 13031

Idea, first put the entire input into a string, and then parse the string, so it will run faster.

1 intGetnum (Const Char* STR,int*num)//input: str---string pointer, num---the array pointer to hold the number returns: number of digits2 {3     intlen=strlen (str);4     intindex=0;5     intT;6      for(intI=0; i<len;i++)7     {8          while(! (str[i]>'0'&&str[i]<'9'))9         {Teni++; One         } A          while(str[i]>='0'&&str[i]<'9') -         { -t=str[i]-'0'; thenum[index]=num[index]*Ten+T; -i++; -         } -index++; +     } -     returnindex; +}

Extracting numbers from a string in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.