In: Computer Science
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one spot in the string. This will overwrite the punctuation characters, resulting in: ['C', 'p', 't', 'S', '1', '2', '1', 'i', 's', 'f', 'u', 'n', '\0']. In this case, the function returns 3. Note: if the srtring does not contain any punctuation characters, then the string is unchanged and the function returns 0.
Please write in C
Please follow the below code with inline comments:
Program 1:
As per the question remove_punct() function should remove the punctuation marks and make the string free from those punctuations.
#include <stdio.h>
int remove_punct(char str_in[]){
int i = 0,no_of_punct = 0,j=0;
char temp[100];// temp variable to store the result
while(str_in[i] != '\0'){
if(str_in[i] == ','|| str_in[i] == '!' || str_in[i] == '.'){//
chcking whether the character in the string is punctuation or not
if true increment the 'no_of_punct'
no_of_punct++;
}
else{
temp[j++] = str_in[i]; // pushing the character by character if it
not a punctuation
}
i++;
}// temp has the resultant string after removing punctuation
return no_of_punct;
}
int main()
{
char str[] = "CptS,121.isfun!"; // this is the input string
printf("%d",remove_punct(str)); // displaying the return value from
remove_punct() function
return 0;
}
output:
Program 2:
#include <stdio.h>
int remove_punct(char str_in[]){
int i = 0,no_of_punct = 0;
while(str_in[i] != '\0'){
if(str_in[i] == ','|| str_in[i] == '!' || str_in[i] == '.'){//
chcking whether the character in the string is punctuation or not
if true increment the 'no_of_punct'
no_of_punct++;
}
i++;
}
return no_of_punct;
}
int main()
{
char str[] = "CptS,121.isfun!"; // this is the input string
printf("%d",remove_punct(str)); // displaying the return value from
remove_punct() function
return 0;
}
output:
as per the question the input string should be modified but it is not returned. in the above code we are counting the no of punctuation marks.