首頁 > 後端開發 > C++ > 在C語言中編寫一個程序,用於檢查一個字串是否包含任何特殊字符

在C語言中編寫一個程序,用於檢查一個字串是否包含任何特殊字符

王林
發布: 2023-09-04 17:45:14
轉載
1244 人瀏覽過

在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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板