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
A loop is designed that goes through a list composed of the characters and punctuations of that string, removes the punctuation and then joins them.
C++
// CPP program to remove punctuation from a given string #include <iostream> using namespace std; int main() { // input string std::string str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks;"; for (int i = 0, len = str.size(); i < len; i++) { // check whether parsing character is punctuation or not if (ispunct(str[i])) { str.erase(i--, 1); len = str.size(); } } // print string without punctuation std::cout << str; return 0; } |
Java
// Java program to remove punctuation from a given string public class Test { public static void main(String[] args) { // input string String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks;"; // similar to Matcher.replaceAll str = str.replaceAll("\\p{Punct}",""); System.out.println(str); } } // This code is contributed by Gaurav Miglani |
Python
# Python program to remove punctuation from a given string # Function to remove punctuation def Punctuation(string): # punctuation marks punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' # traverse the given string and if any punctuation # marks occur replace it with null for x in string.lower(): if x in punctuations: string = string.replace(x, "") # Print string without punctuation print(string) # Driver program string = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks;"Punctuation(string) |
C#
// C# program to remove punctuation // from a given string using System; using System.Text.RegularExpressions; class GFG { public static void Main() { // input string String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks;"; // similar to Matcher.replaceAll str = Regex.Replace(str,@"[^\w\d\s]",""); Console.Write(str); } } // This code is contributed // by 29AjayKumar |
Output:
Welcome to GeeksforGeeks
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print string after removing all (“10” or “01”) from the binary string
- Removing string that is an anagram of an earlier string
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Minimize a string by removing all occurrences of another string
- Check if string remains palindrome after removing given number of characters
- Minimize cost to empty a given string by removing characters alphabetically
- Check if permutation of a given string can be made palindromic by removing at most K characters
- Minimum sum of squares of character counts in a given string after removing k characters
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Lexicographically smallest subsequence possible by removing a character from given string
- Removing spaces from a string using Stringstream
- Find longest palindrome formed by removing or shuffling chars from string
- Character replacement after removing duplicates from a string
- Ways of transforming one string to other by removing 0 or more characters
- Modify string by removing vowels in between two consonants
- Balance a string after removing extra brackets
- Minimize the length of string by removing occurrence of only one character
- Reduce the string by removing K consecutive identical characters
- Lexicographically smallest string formed by removing at most one character
- Find the resultant String after replacing X with Y and removing Z
Improved By : 29AjayKumar

