

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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\n"); flag = 1; break; } } //if there is no special charcter if (flag == 0){ printf("string is accepted\n"); } 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
- Related Questions & Answers
- C# program to check if a string contains any special character
- Java program to check if a string contains any special character
- Program to check if a string contains any special character in Python
- Python program to check if a string contains any unique character
- PHP program to check if a string has a special character
- Check if string contains special characters in Swift
- Java Program to check if the String contains any character in the given set of characters
- C# Program to replace a special character from a String
- Check if a string contains a sub-string in C++
- Java Program to Check if a string contains a substring
- C# Program to check if a character is a whitespace character
- How to check if a string contains only one type of character in R?
- Python program to check if a string contains all unique characters
- How to check if a string contains a certain word in C#?
- Check if a string contains numbers in MySQL?