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

1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
#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 value returning function called isPrime. This function accepts integer number as parameter and checks...
Write a value returning function called isPrime. This function accepts integer number as parameter and checks whether it is prime or not. If the number is prime the function returns true. Otherwise, function returns false. A prime number is the number that can be divided by itself and 1 without any reminder, i.e. divisible by itself and 1 only. DO THIS USING C++ LANGUAGE .WITH UPTO CHAPTERS 5 (LOOP).
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...
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function...
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function checks if two adjacent characters in the string are the same; if they are the same, then return True otherwise return False.   The function should ignore case and check for non-empty string.  If it's an empty string, then return the message 'Empty string'. Sample runs: print( isAdjacent('ApPle')) # True print( isAdjacent('Python')) # False print( isAdjacent('')) # Empty string
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT