Lab 7-3-7 Character Conversion (15 points)

Lab 7-3-7 Character Conversion (15 points)
This question asks to extract all numeric characters ('0'...'9') in a string and convert it to an integer output.

Input format:
Input gives a string of no more than 80 characters on one line terminated by a carriage return.

Output format:
Output the converted integer in one line. The title guarantees that the output does not exceed the range of long integers.

Input sample:
free82jeep5

Sample output:
825

#include<stdio.h>
#include<string.h>
#define N 85
//Idea: Use a character array to store the character form of the number, and then process the character into a number.
int main(void)
{
	int i, j, temp=1, flag, len1 = 0, len2 = 0, sum = 0;
	char ch;
	char str1[N]; //Store the input string
	char str2[N]; //Store the processed string

	ch = getchar(); //input string
	for (i = 0; ch != '\n'; i++)
	{
		str1[i] = ch;
		len1 ++;
		ch = getchar();
	}
	for (i = 0; i < len1; i++) //Preliminarily process the string to get a character array from '0' to '9'
	{
		if (str1[i] >= '0' && str1[i] <= '9')
		{
			str2[len2] = str1[i]; //Classic: Get the required characters in the string. Set the counter, and add 1 to len2 only when a capital letter is found
			len2++; //Record the length of capital letters
		}
	}
	for (i = len2-1; i >= 0; i--)
	{
		sum += (str2[i] - '0') * temp;
		temp *= 10;
	}
	printf("%d\n", sum);
	return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690971&siteId=291194637