# Program to check if a string contains any special character in C

Given a string str[], the task is to check whether the string contains any special character and if the string have a special character then print “The String is not accepted” else print “The string is accepted”.

Special characters are those characters which are neither numeric nor alphabetic i.e. − !@#$%^&*()+=-\][‘;/.,{}|:”<>?~ So in C Programming language we will use if-else approach to solve the problem. Input − str[] = {“tutorials-point”} Output − the string is not accepted Input − str[] = {“tutorialspoint”} Output − The string is accepted ## Approach used below is as follows to solve the problem − • Traverse the whole string. • Will look for the special character, if the special character does exist in the string then, print “The string is not accepted and break”. Else, print the string is accepted. ## Other approach If we are coding in java or any other language which supports the concept of regular expressions, then instead of if-else approach we will use regular expressions to check that whether they are present in the given string or not. This is not only a simple approach but also a fast one. ## Algorithm 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

## Example

Live Demo

#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");
flag = 1;
break;
}
}
//if there is no special charcter
if (flag == 0){
printf("string is accepted");
}
return 0;
}
int main(int argc, char const *argv[]){
char str[] = {"Tutorials-point"};
int n = strlen(str);
special_character(str, n);
return 0;
}

## Output

If run the above code it will generate the following output −

String is not allowed`

Updated on: 13-Aug-2020

5K+ Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements