Home >Backend Development >C++ >Write a program in C language to check if a string contains any special characters

Write a program in C language to check if a string contains any special characters

王林
王林forward
2023-09-04 17:45:141251browse

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

Here's how to solve the problem:

  • 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.

Other methods

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.

Algorithm

Start
In function int special_character(char str[], int n)
   Step 1→ initialize i and flag and set flag as 0
   Step 2&rarr; Loop For i = 0 and i < n and ++i
      If(str[i] == &#39;!&#39; || str[i] == &#39;@&#39; || str[i] == &#39;#&#39; || str[i] == &#39;$&#39;
      || str[i] == &#39;%&#39; || str[i] == &#39;^&#39; || str[i] == &#39;&&#39; || str[i] == &#39;*&#39;
      || str[i] == &#39;(&#39; || str[i] == &#39;)&#39; || str[i] == &#39;-&#39; || str[i] == &#39;{&#39;
      || str[i] == &#39;}&#39; || str[i] == &#39;[&#39; || str[i] == &#39;]&#39; || str[i] == &#39;:&#39;
      || str[i] == &#39;;&#39; || str[i] == &#39;"&#39; || str[i] == &#39;\&#39;&#39; || str[i] == &#39;<&#39;
      || str[i] == &#39;>&#39; || str[i] == &#39;.&#39; || str[i] == &#39;/&#39; || str[i] == &#39;?&#39;
      || str[i] == &#39;~&#39; || str[i] == &#39;`&#39; then
         Print "String is not allowed&rdquo;
            Set flag as 1
         break
   Step 3&rarr; If flag == 0 then,
      Print "string is accepted&rdquo;
In function int main(int argc, char const *argv[])
   Step 1&rarr; Declare and set str[] as {"Tutorials-point"}
   Step 2&rarr; set n as strlen(str)
   Step 3&rarr; special_character(str, n)
Stop

Example

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] == &#39;!&#39; || str[i] == &#39;@&#39; || str[i] == &#39;#&#39; || str[i] == &#39;$&#39;
      || str[i] == &#39;%&#39; || str[i] == &#39;^&#39; || str[i] == &#39;&&#39; || str[i] == &#39;*&#39;
      || str[i] == &#39;(&#39; || str[i] == &#39;)&#39; || str[i] == &#39;-&#39; || str[i] == &#39;{&#39;
      || str[i] == &#39;}&#39; || str[i] == &#39;[&#39; || str[i] == &#39;]&#39; || str[i] == &#39;:&#39;
      || str[i] == &#39;;&#39; || str[i] == &#39;"&#39; || str[i] == &#39;\&#39;&#39; || str[i] == &#39;<&#39;
      || str[i] == &#39;>&#39; || str[i] == &#39;.&#39; || str[i] == &#39;/&#39; || str[i] == &#39;?&#39;
      || str[i] == &#39;~&#39; || str[i] == &#39;`&#39; ){
         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;
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete