Question: For this question you will write code for the following function: char * onlyDigits ( char * str ) This function returns a dynamically created

For this question you will write code for the following function:
char* onlyDigits(char *str)
This function returns a dynamically created new string which contains only the digits from the
given string. Some examples:
onlyDigits((210)555-1337) should return a new string 2105551337.
onlyDigits(pi =3.14159...) should return a new string 314159.
onlyDigits(HelloW orld) should return a new string .
Some additional requirements for your onlyDigits function:
The new string should have exactly enough size to store the digits in the string and a null
terminator (i.e., dont malloc more memory than you need). Remember that \0 is the null
terminator character which ends a string.
If your malloc fails (i.e. it returns N U LL), your function should call exit(-1).
The only functions your onlyDigits implementation should call are numDigits, malloc, sizeof ,
exit, and strlen.
(a)(0 points) Complete the following helper function which returns the number of digits in the
given string. This should be a useful function when writing onlyDigits.
Solution:
//Return the number of digits in the given string
int numDigits( char *str )
{
int i =0;
int count =0;
int size = strlen( str );
for( i=0; i=0 && str[i]<=9)
count++;
}
return count;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!