Implement a function with the following prototype: char* findAllDigits(char* str
ID: 3828506 • Letter: I
Question
Implement a function with the following prototype: char* findAllDigits(char* str); This function scans the input string and returns its digital characters as one string. For example, when the input is "April 28th, 11am", this function returns "2811"; when there is no digital character, it returns NULL Extra credit: Based on the function of problem 4, develop one function with the following prototype: char* findsimilarchar(char* str, int type); The second parameter type specifies the category of character to be returned:Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* findAllDigits(char* str)
{
int numOfDigits = 0;
for(int i = 0; i < strlen(str); i++)
if(*(str + i) >= '0' && *(str + i) <= '9')
numOfDigits++;
char *result = malloc(sizeof(char) * numOfDigits + 1);
int j = 0;
for(int i = 0; i < strlen(str); i++)
if(*(str + i) >= '0' && *(str + i) <= '9')
{
*(result + j) = *(str + i);
j++;
}
*(result + j) = '';
return result;
}
char* findAllUppercaseChars(char* str)
{
int numOfUppers = 0;
for(int i = 0; i < strlen(str); i++)
if(*(str + i) >= 'A' && *(str + i) <= 'Z')
numOfUppers++;
char *result = malloc(sizeof(char) * numOfUppers + 1);
int j = 0;
for(int i = 0; i < strlen(str); i++)
if(*(str + i) >= 'A' && *(str + i) <= 'Z')
{
*(result + j) = *(str + i);
j++;
}
*(result + j) = '';
return result;
}
char* findAllLowercaseChars(char* str)
{
int numOfLowers = 0;
for(int i = 0; i < strlen(str); i++)
if(*(str + i) >= 'a' && *(str + i) <= 'z')
numOfLowers++;
char *result = malloc(sizeof(char) * numOfLowers + 1);
int j = 0;
for(int i = 0; i < strlen(str); i++)
if(*(str + i) >= 'a' && *(str + i) <= 'z')
{
*(result + j) = *(str + i);
j++;
}
*(result + j) = '';
return result;
}
char* findSimilarChar(char* str, int type)
{
if(type == 1)
return findAllDigits(str);
else if(type == 2)
return findAllUppercaseChars(str);
else
return findAllLowercaseChars(str);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.