给定一个字符串 str[],任务是检查字符串是否包含任何特殊字符,如果字符串有特殊字符,则打印“字符串不被接受”,否则打印“字符串被接受”。
特殊字符是那些既不是数字也不是字母的字符,即 – !@#$%^&*()+=-][‘;/.,{}|:”?`~
因此,在C编程语言中,我们将使用if-else方法来解决问题。
输入 – str[] = {“tutorials-point”}
立即学习“C语言免费学习笔记(深入)”;
输出 – 字符串不被接受
输入 – str[] = {“tutorialspoint”}
输出 – 字符串被接受
以下是解决问题的方法:
遍历整个字符串。
查找特殊字符,如果字符串中存在特殊字符,则打印“字符串不被接受并中断”。否则,打印字符串被接受。
其他方法
如果我们在Java或任何其他支持正则表达式概念的语言中编码,那么我们将使用正则表达式来检查给定字符串中是否存在它们。这不仅是一种简单的方法,而且速度快。
算法
StartIn 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 ' || 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
登录后复制
示例
实时演示
#include #include int special_character(char str[], int n){ int i, flag = 0; for (i = 0; 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;}
登录后复制
输出
如果运行上述代码,将生成以下输出−
String is not allowed
登录后复制
以上就是在C语言中编写一个程序,用于检查一个字符串是否包含任何特殊字符的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2583862.html