Question

In: Computer Science

Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...

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

Solutions

Expert Solution

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.


Related Solutions

#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a function called format_name that accepts a string in the format of first name followed...
Write a function called format_name that accepts a string in the format of first name followed by last name as a parameter, and then returns a string in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. For example, format_name("Jared Smith") should return "Smith, Jared" Hint: The following String Methods will be useful: # Returns the lowest index in the string where substring is # found. Returns -1 if...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it...
a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it constructs a pattern of a diamond of height n If n is even, it constructs an hourglass of length n b) What is the time complexity of the drawShape() function you created? C++ language with for loop
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT