Program to check if a string contains any special character

12 Jun, 2022
Given a string, the task is to check if that string contains any special character (defined special character set). If any special character found, don’t accept that string.Examples :  
Input : Geeks$For$Geeks
Output : String is not accepted.

Input : Geeks For Geeks
Output : String is accepted
 
Approach : Make a regular expression(regex) object of all the special characters that we don’t want, then pass a string in search method. If any one character of string is matching with regex object then search method returns a match object otherwise return None.Below is the implementation :  
C++
Python3
PHP
// C++ program to check if a string// contains any special character // import required packages#include <iostream>#include <regex>using namespace std; // Function checks if the string// contains any special charactervoid run(string str){         // Make own character set    regex regx("[@_!#$%^&*()<>?/|}{~:]");     // Pass the string in regex_search    // method    if(regex_search(str, regx) == 0)        cout << "String is accepted";    else        cout << "String is not accepted.";} // Driver Codeint main(){         // Enter the string    string str = "Geeks$For$Geeks";         // Calling run function    run(str);     return 0;} // This code is contributed by Yash_R
# Python3 program to check if a string# contains any special character # import required packageimport re # Function checks if the string# contains any special characterdef run(string):     # Make own character set and pass    # this as argument in compile method    regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')         # Pass the string in search    # method of regex object.       if(regex.search(string) == None):        print("String is accepted")             else:        print("String is not accepted.")      # Driver Codeif __name__ == '__main__' :         # Enter the string    string = "Geeks$For$Geeks"         # calling run function    run(string)
<?Php// PHP program to check if a string// contains any special character // Function checks if the string// contains any special characterfunction run($string){    $regex = preg_match('[@_!#$%^&*()<>?/\|}{~:]',                                         $string);    if($regex)        print("String is accepted");             else        print("String is not accepted.");} // Driver Code // Enter the string$string = 'Geeks$For$Geeks'; // calling run functionrun($string); // This code is contributed by Aman ojha?>
String is not accepted.
Method:  To check if a special character is present in a given string or not, firstly group all special characters as one set. Then using for loop and if statements check for special characters. If any special character is found then increment the value of c. Finally, check if the c value is greater than zero then print string is not accepted otherwise print string is accepted. 
Python3
# Python code# to check if any special character is present#in a given string or not # input stringn="Geeks$For$Geeks"n.split()c=0s='[@_!#$%^&*()<>?/\|}{~:]' # special character setfor i in range(len(n)):    # checking if any special character is present in given string or not    if n[i] in s:      c+=1   # if special character found then add 1 to the c # if c value is greater than 0 then print no# means special character is found in string     if c:    print("string is not accepted")else:    print("string accepted") # this code is contributed by gangarajula laxmi
string is not accepted
 
Aman ojha
Yash_R
mallapraveen2014
laxmigangarajula03
Python Regex-programs
Python string-programs
python-regex
python-string
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.