![]() |
VOOZH | about |
Given a string, remove the punctuation from the string if the given character is a punctuation character, as classified by the current C locale. The default C locale classifies these characters as punctuation:
! " # $ % & ' ( ) * + , - . / : ; ? @ [ \ ] ^ _ ` { | } ~
Examples:
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went
Approach: First check the input string if it consists of punctuations then we have to make it punctuation free. In order to do this, we will traverse over the string, and if punctuations are found we will remove them. Let's say the input string is 'Student' then we have to remove $ and @, furthermore we have to print the plain string 'Student' which is free from any punctuations.
Algorithm:
- Initialize the input string
- Check if the character present in the string is punctuation or not.
- If a character is a punctuation, then erase that character and decrement the index.
- Print the output string, which will be free of any punctuation.
Below is the implementation of the above approach:
Welcome to GeeksforGeeks
Time Complexity: O(n2)
Auxiliary Space: O(1)
Approach 2 : - Using a loop to iterate over the string and remove punctuations
Initialize an empty string called result.
Iterate over the characters in the given string using a loop.
For each character, check if it is a punctuation character using the ispunct function.
If the character is not a punctuation character, add it to the result string.
Repeat steps 3-4 for all characters in the string.
Replace the original string with the result string, effectively removing all punctuation characters from the original string.
This approach may be useful in cases where you need to perform additional operations on each character in the string before removing punctuations, or if you need to process the string character-by-character rather than using a built-in algorithm.
Welcome to GeeksforGeeks
Time Complexity: O(n), where n is the size of the string
Auxiliary Space: O(1)