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 onlyDigitschar str
This function returns a dynamically created new string which contains only the digits from the
given string. Some examples:
onlyDigits should return a new string
onlyDigitspi should return a new string
onlyDigitsHelloW 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 ie dont malloc more memory than you need Remember that is the null
terminator character which ends a string.
If your malloc fails ie it returns N U LL your function should call exit
The only functions your onlyDigits implementation should call are numDigits, malloc, sizeof
exit, and strlen.
a 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 ;
int count ;
int size strlen str ;
for i; i && stri
count;
return count;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
