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:
C++
#include <iostream>
using namespace std;
int main()
{
std::string str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
for ( int i = 0, len = str.size(); i < len; i++)
{
if (ispunct(str[i]))
{
str.erase(i--, 1);
len = str.size();
}
}
std::cout << str;
return 0;
}
|
Java
public class Test
{
public static void main(String[] args)
{
String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
str = str.replaceAll( "\\p{Punct}" , "" );
System.out.println(str);
}
}
|
Python3
def Punctuation(string):
punctuations =
for x in string.lower():
if x in punctuations:
string = string.replace(x, "")
print (string)
string = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks"
Punctuation(string)
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
public static void Main()
{
String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
str = Regex.Replace(str, @"[^\w\d\s]" , "" );
Console.Write(str);
}
}
|
Javascript
<script>
{
var str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
str = str.replace(/[^a-zA-Z ]/g, "" );
document.write(str);
}
</script>
|
Output
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.
C++
#include <iostream>
using namespace std;
string removePunctuations(std::string& s) {
std::string result = "" ;
for ( char c : s) {
if (!ispunct(c)) {
result += c;
}
}
s = result;
return s;
}
int main()
{
std::string str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
std::cout << removePunctuations(str);
return 0;
}
|
Java
import java.util.*;
public class RemovePunctuations {
public static String removePunctuations(String s) {
StringBuilder result = new StringBuilder();
for ( char c : s.toCharArray()) {
if (Character.isLetterOrDigit(c) || Character.isWhitespace(c)) {
result.append(c);
}
}
return result.toString();
}
public static void main(String[] args) {
String str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
System.out.println(removePunctuations(str));
}
}
|
Python3
import re
def removePunctuations(s):
result = ""
for c in s:
if not re.match(r "[.,\/#!$%\^&\*;:{}=\-_`~()@?]" , c): # If c is not a punctuation character
result + = c
s = result
return s
str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks"
print (removePunctuations( str ))
|
C#
using System;
using System.Text.RegularExpressions;
class Program {
static void Main( string [] args)
{
string str
= "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
Console.WriteLine(RemovePunctuations(str));
}
static string RemovePunctuations( string s)
{
string result = "" ;
foreach ( char c in s)
{
if (!Regex.IsMatch(
c.ToString(),
@"[.,\/#!$%\^&\*;:{}=\-_`~()@?]" ))
{
result += c;
}
}
s = result;
return s;
}
}
|
Javascript
function removePunctuations(s) {
let result = "" ;
for (let c of s) {
if (!c.match(/[.,\/ #!$%\^&\*;:{}=\-_`~()@?]/g)) { // If c is not a punctuation character
result += c;
}
}
s = result;
return s;
}
let str = "Welcome???@@##$ to#$% Geeks%$^for$%^&Geeks" ;
console.log(removePunctuations(str));
|
Output
Welcome to GeeksforGeeks
Time Complexity: O(n), where n is the size of the string
Auxiliary Space: O(1)
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Apr, 2023
Like Article
Save Article