Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Programming in c language. As a string input, the program will turn it into an integer, such as 126x, where n is 126, amc and n is 0.
Programming in c language. As a string input, the program will turn it into an integer, such as 126x, where n is 126, amc and n is 0.
Do you want to ignore characters other than numbers and leave only numbers, and then convert them into integers?

# Contains? & ltstdio.h & gt

# Contains? & ltstring.h & gt

int? str2Num(char? * str);

int? p 10(int? m); //Calculate the power of 10.

int? Master ()

{

Charles? str[ 100];

while( 1)

{

Printf ("Enter a character:");

scanf("%s ",str);

Printf ("Ignore characters other than numbers, and convert the remaining characters into integer values: %d\n", str2num (str));

}

Return? 0;

}

int? str2Num(char? *str)

{

int? n=strlen(str),m=0,sum = 0;

Charles? * p = & ampstr[n- 1];

While(n-)// Take forward from the last bit of the string and accumulate the numbers.

{

if(* p & gt; ='0'? & amp& amp? * p & lt='9')

{

sum = sum+(* p-' 0 ')* p 10(m);

m++;

}

p-;

}

Return? Sum;

}

int? p 10(int? M)// Calculate the power of 10.

{

int? I,sum = 1;

for(I = 0; I & ltm;; i++)

sum * = 10;

Return? Sum;

}