Removing punctuations from a given string

12 Jul, 2022
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: 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
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 stringCheck 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.
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:
C++
Java
Python3
C#
Javascript
// 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 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 program to remove punctuation from a given string# Function to remove punctuationdef 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 programstring = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks"Punctuation(string)
// C# program to remove punctuation// from a given stringusing 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
<script> // JavaScript program to remove punctuation from a given string    {        // input string        var str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks";                 // similar to Matcher.replaceAll        str = str.replace(/[^a-zA-Z ]/g, "");                 document.write(str);    }     // This code is contributed by shivanisingh </script>
Welcome to GeeksforGeeks
Time Complexity: O(n2) Auxiliary Space: O(1)
This article is contributed by Aarti_Rathi and Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
29AjayKumar
shivanisinghss2110
amartyaghoshgfg
_shinchancode
lucidcoder121
hardikkoriintern
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.