Home >Backend Development >C++ >Write a program in C language to check if a string contains any special characters
Given a string str[], the task is to check if the string contains any special characters and if the string has special characters, print "String is not accepted" ”, otherwise print “String Accepted”.
Special characters are those that are neither numbers nor letters, i.e. - !@#$%^&*() =-\][';/.,{}|:"? `~
So, in C programming language, we will use if-else method to solve the problem.
Input - str[] = {"tutorials-point "}
Output - String not accepted
Input - str[] = {"tutorialspoint"}
Output - String accepted
Iterate over the entire string.
Find special characters, if special characters are present in the string, print "String is not accepted and break". Otherwise, print string is accepted.
If we are coding in Java or any other language that supports the concept of regular expressions, then we will use regular expressions to check whether they are present in a given string. This is not just a simple method, And it’s fast.
Start In function int special_character(char str[], int n) Step 1→ initialize i and flag and set flag as 0 Step 2→ Loop For i = 0 and i < n and ++i If(str[i] == '!' || str[i] == '@' || str[i] == '#' || str[i] == '$' || str[i] == '%' || str[i] == '^' || str[i] == '&' || str[i] == '*' || str[i] == '(' || str[i] == ')' || str[i] == '-' || str[i] == '{' || str[i] == '}' || str[i] == '[' || str[i] == ']' || str[i] == ':' || str[i] == ';' || str[i] == '"' || str[i] == '\'' || str[i] == '<' || str[i] == '>' || str[i] == '.' || str[i] == '/' || str[i] == '?' || str[i] == '~' || str[i] == '`' then Print "String is not allowed” Set flag as 1 break Step 3→ If flag == 0 then, Print "string is accepted” In function int main(int argc, char const *argv[]) Step 1→ Declare and set str[] as {"Tutorials-point"} Step 2→ set n as strlen(str) Step 3→ special_character(str, n) Stop
Real-time demonstration
#include <stdio.h> #include <string.h> int special_character(char str[], int n){ int i, flag = 0; for (i = 0; i < n; ++i){ //checking each character of the string for special character. if(str[i] == '!' || str[i] == '@' || str[i] == '#' || str[i] == '$' || str[i] == '%' || str[i] == '^' || str[i] == '&' || str[i] == '*' || str[i] == '(' || str[i] == ')' || str[i] == '-' || str[i] == '{' || str[i] == '}' || str[i] == '[' || str[i] == ']' || str[i] == ':' || str[i] == ';' || str[i] == '"' || str[i] == '\'' || str[i] == '<' || str[i] == '>' || str[i] == '.' || str[i] == '/' || str[i] == '?' || str[i] == '~' || str[i] == '`' ){ printf("String is not allowed</p><p>"); flag = 1; break; } } //if there is no special charcter if (flag == 0){ printf("string is accepted</p><p>"); } return 0; } int main(int argc, char const *argv[]){ char str[] = {"Tutorials-point"}; int n = strlen(str); special_character(str, n); return 0; }
If you run the above code, the following output will be generated −
String is not allowed
The above is the detailed content of Write a program in C language to check if a string contains any special characters. For more information, please follow other related articles on the PHP Chinese website!