首页 > 后端开发 > C++ > 在C语言中编写一个程序,用于检查一个字符串是否包含任何特殊字符

在C语言中编写一个程序,用于检查一个字符串是否包含任何特殊字符

王林
发布: 2023-09-04 17:45:14
转载
1280 人浏览过

在C语言中编写一个程序,用于检查一个字符串是否包含任何特殊字符

给定一个字符串 str[],任务是检查字符串是否包含任何特殊字符,如果字符串有特殊字符,则打印“字符串不被接受”,否则打印“字符串被接受”。

特殊字符是那些既不是数字也不是字母的字符,即 - !@#$%^&*()+=-][‘;/.,{}|:"?`~

因此,在C编程语言中,我们将使用if-else方法来解决问题。

输入 - str[] = {"tutorials-point"}

输出 - 字符串不被接受

输入 - str[] = {"tutorialspoint"}

输出 - 字符串被接受

以下是解决问题的方法:

  • 遍历整个字符串。

  • 查找特殊字符,如果字符串中存在特殊字符,则打印“字符串不被接受并中断”。否则,打印字符串被接受。

其他方法

如果我们在Java或任何其他支持正则表达式概念的语言中编码,那么我们将使用正则表达式来检查给定字符串中是否存在它们。这不仅是一种简单的方法,而且速度快。

算法

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
登录后复制

示例

 实时演示

#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;
}
登录后复制

输出

如果运行上述代码,将生成以下输出−

String is not allowed
登录后复制

以上是在C语言中编写一个程序,用于检查一个字符串是否包含任何特殊字符的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
用php如何生成小程序的小程序码?
来自于 1970-01-01 08:00:00
0
0
0
小程序可以用来开发功能性程序吗?
来自于 1970-01-01 08:00:00
0
0
0
微信小程序
来自于 1970-01-01 08:00:00
0
0
0
程序检测到端口80已经被httped.exe进程占用!
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板