Dada una string, elimine la puntuación de la string si el carácter dado es un carácter de puntuación, según la clasificación de la configuración regional actual de C. La configuración regional predeterminada de C clasifica estos caracteres como puntuación:
! " # $ % & ' ( ) * + , - . / : ; ? @ [ \ ] ^ _ ` { | } ~
Ejemplos:
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
Python3
# 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
Javascript
<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>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA