Recognize integers in a string and convert them to numbers

Source: Internet
Author: User

Recognize integers in a string and convert them to numbers (40 points)

Problem description:
Recognize All integers in the input string, count the number of integers, and convert these integers into numbers.

Required implementation functions:
Void take_num (const char * strin, int * n, unsigned int * outarray)

[Input] strin: the input string

[Output] n: count the number of identified integers.

Outarray: The recognized integer. outarray [0] is the first integer from left to right in the input string,

Outarray [1] is the second integer, and so on. The array address has been allocated and can be used directly.

[Return] None

Note:

I. do not consider the positive and negative signs (+,-) in the string, that is, all the conversion results are non-negative integers (including 0 and positive integers)

II. If the converted integer is out of the range, the maximum integer that may appear in the test case will not exceed the range that the unsigned int can process.

Iii. Consider the number string starting with '0'. For example, "00035", it should be converted to an integer of 35;

"000" should be converted to integer 0; "00.0035" should be converted to integer 0 and 35 (ignore decimal point: Mmm. NNN as two numbers mmm And NNN for identification)

IV. The input string does not exceed 100 bytes. do not consider extra-long strings.

Example
Input: strin = "ab00cd + 123fght456-25 3.005fgh"

Output: n = 6

Outarray = {0,123,456, 25, 3, 5}

 

 1 #include<stdio.h> 2 #include<stdlib.h> 3 void take_num(const char *strIn, int *n, unsigned int *outArray) 4 { 5     int i,j,sum; 6     const char *p = strIn; 7     i = 0; 8     while (*p != ‘\0‘) 9     {10         sum = 0;11         while(*p < ‘0‘ || *p > ‘9‘)12         {   if (*p == ‘\0‘)13                 break;14             p++;15         }16         while (*p >= ‘0‘ && *p <= ‘9‘ )17         {18             if (*p == ‘\0‘)19                 break;20             j = *p - ‘0‘;21             sum = sum * 10 + j;22             p++;23         }24         outArray[i] = sum;25         i++;26         if (*p == ‘\0‘)27             break;28         p++;29     }30     if(*(p-1) >= ‘0‘ && *(p-1) <= ‘9‘)31         *n = i;32     else33         *n = i-1;34 }35 int main()36 {37     char str[100];38     int *n;39     unsigned int out[100];40     n = (int*)malloc(sizeof(int));41     //scanf("%s",str);42     gets(str);43     take_num(str,n,out);44     printf("%d\n",*n);45     for (int i=0; i < *n; i++)46         printf("%d ",out[i]);47     printf("\n");48     return 0;49 }

 

 

 

Determine the end mark of the string.

Recognize integers in a string and convert them to numbers

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.