Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรมตรวจสอบว่าสตริงมีอักขระพิเศษใน C . หรือไม่


ให้สตริง str[] ภารกิจคือการตรวจสอบว่าสตริงมีอักขระพิเศษหรือไม่ และหากสตริงมีอักขระพิเศษ ให้พิมพ์ "The String is not suitable" มิฉะนั้นให้พิมพ์ " สตริงเป็นที่ยอมรับ”

อักขระพิเศษคืออักขระที่ไม่ใช่ตัวเลขหรือตัวอักษร เช่น − !@#$%^&*()+=-\][‘;/.,{}|:”<>?`~

ดังนั้นในภาษา C Programming เราจะใช้วิธี if-else เพื่อแก้ปัญหา

ป้อนข้อมูล − str[] ={“tutorials-point"}

ผลลัพธ์ - ไม่รับสตริง

ป้อนข้อมูล − str[] ={“tutorialspoint"}

ผลผลิต − สตริงได้รับการยอมรับ

แนวทางที่ใช้ด้านล่างมีดังต่อไปนี้ในการแก้ปัญหา -

  • ข้ามทั้งสตริง

  • จะมองหาอักขระพิเศษ หากมีอักขระพิเศษอยู่ในสตริง ให้พิมพ์ว่า "The string is not accept and break" มิฉะนั้น พิมพ์สตริงได้รับการยอมรับ

แนวทางอื่นๆ

หากเรากำลังเขียนโค้ดในภาษาจาวาหรือภาษาอื่นใดที่สนับสนุนแนวคิดของนิพจน์ทั่วไป แทนที่จะใช้วิธี if-else เราจะใช้นิพจน์ทั่วไปเพื่อตรวจสอบว่านิพจน์นั้นมีอยู่ในสตริงที่กำหนดหรือไม่ นี่ไม่ใช่แค่วิธีการง่ายๆ แต่ยังเป็นวิธีที่รวดเร็วอีกด้วย

อัลกอริทึม

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

ตัวอย่าง

#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;
}

ผลลัพธ์

หากรันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้ -

String is not allowed