C language-Enter a string to convert consecutive numeric characters to numbers

Source: Internet
Author: User

Enter a string with numeric and non-numeric characters, for example:

A123CDF 456.78cpc876.9er 849.1

The successive numbers are used as a real number, which is stored in an array a . For example 123 stored in a[0],456.78 stored in a[2], and so on, Statistics total number of the number, and output these numbers.

#include <stdio.h>

#include <string.h>

#include <math.h>

#define N 50

int number (char str[],float num[]);

int main () {

Char Str[n];

float Num[n];

int k,i;

Gets (str);

K=number (Str,num);

printf ("%d number \ n", k);

for (i=0; i<k; i++)

{

if (num[i]-(int) num[i]==0)

printf ("%.0f", Num[i]);

else printf ("%g", Num[i]);

}

return 0;

}

int number (char str[],float num[]) {

int i,j;

int k=0;

int t;

float sum=0;

while (str[i]! = ' + ')

{

if (str[i]>= ' 0 ' && str[i]<= ' 9 ') {

sum=0;

while (str[i]>= ' 0 ' && str[i]<= ' 9 ')

{

sum=sum*10+str[i]-' 0 ';

i++;

if (str[i]== '. ')

{

t=i;

i++;

while (str[i]>= ' 0 ' && str[i]<= ' 9 ')

{

sum=sum+ (str[i]-' 0 ') *pow (10,t-i);

i++;

}

}

}

Num[k]=sum;

k++;

}

if (str[i]== '. ')

{

sum=0;

t=i;

i++;

while (str[i]>= ' 0 ' && str[i]<= ' 9 ')

{

sum=sum+ (str[i]-' 0 ') *pow (10,t-i);

i++;

}

Num[k]=sum;

k++;

}

i++;

}

return k;

}

C language-Enter a string to convert consecutive numeric characters 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.