1 / 37

การประมวลผลสายอักขระ

การประมวลผลสายอักขระ. (String Processing). Outline. ทบทวน String การประมวลผลสายอักขระ (String Processing) Pattern Matching Encryption/Decryption Compression. ทบทวน String. การประกาศตัวแปรเพื่อเก็บข้อความ การรับและการแสดงผลข้อความ การกำหนดค่าให้กับตัวแปรเก็บข้อความ

zinnia
Télécharger la présentation

การประมวลผลสายอักขระ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. การประมวลผลสายอักขระ (String Processing)

  2. Outline • ทบทวน String • การประมวลผลสายอักขระ (String Processing) • Pattern Matching • Encryption/Decryption • Compression

  3. ทบทวน String • การประกาศตัวแปรเพื่อเก็บข้อความ • การรับและการแสดงผลข้อความ • การกำหนดค่าให้กับตัวแปรเก็บข้อความ • ฟังก์ชันที่ใช้กับข้อความและอักขระ

  4. การประกาศตัวแปรเพื่อเก็บข้อความการประกาศตัวแปรเพื่อเก็บข้อความ • รูปแบบ char ชื่อตัวแปร[จำนวนอักขระ] โดยที่จำนวนอักขระต้องมากกว่าจำนวนอักขระที่เก็บจริง 1 ช่อง เพราะช่องสุดท้ายต้องเก็บอักขระ NULL ซึ่งเขียนแทนด้วย‘\0’ เพื่อบอกให้ตัวแปลภาษารู้ว่าเป็นข้อความ • ตัวอย่าง char name[10]; หมายถึง ตัวแปรชื่อ name เก็บข้อความยาว 9 อักขระ char color[ ]; หมายถึง ตัวแปรชื่อ color เก็บข้อความโดยไม่กำหนดขนาด ซึ่งในกรณีนี้ตัวแปลภาษาจะกำหนดขนาดให้เท่ากับจำนวนอักขระบวก 1

  5. การรับข้อมูล การรับข้อความโดยใช้ฟังก์ชัน scanf() • รูปแบบ:scanf (“%s”, ชื่อตัวแปร) • ตัวอย่าง #include <stdio.h> char massage[ 20]; void main ( ) { scanf (“%s”, message); printf (“%s”, message); }

  6. การรับข้อมูล รับข้อมูลทีละตัวอักษรด้วยฟังก์ชัน getchar ( ) • การทำงาน>>เมื่อผู้ใช้กรอกตัวอักษรแล้ว จะต้องกดปุ่ม Enter โปรแกรมจึงจะกลับไปทำงานต่อ โดยอักขระที่ผู้ใช้กรอก จะปรากฏขึ้นมาให้เห็นบนหน้าจอด้วย • รูปแบบ: ชื่อตัวแปร = getchar() • ตัวอย่าง #include <stdio.h> void main ( ) { char ch; ch = getchar(); printf("You type a character is ...%c \n",ch); }

  7. การรับข้อมูล รับข้อความด้วยฟังก์ชัน gets ( ) • รูปแบบ:gets(ชื่อตัวแปร) • ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str); printf("You type a string is ...%s \n",str); }

  8. การแสดงผลลัพธ์ การแสดงผลข้อความโดยใช้ฟังก์ชัน printf • รูปแบบ: printf (“%s”, ชื่อตัวแปร); • ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str); printf("You type a string is ...%s \n",str); }

  9. การแสดงผลลัพธ์ การแสดงผลทีละตัวอักษรโดยใช้ฟังก์ชัน printf • รูปแบบ: printf (“%c”, ชื่อตัวแปร); • ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str); for(i=0;i<20;i++) printf("You type a string is ...%c \n",str[i] ); }

  10. การแสดงผลลัพธ์ การแสดงผลทีละตัวอักษรด้วยฟังก์ชัน putchar() รูปแบบ:putchar (argument) argument : ตัวแปร, ค่าคงที่ , ฟังก์ชัน ตัวอย่าง #include <stdio.h> void main ( ) { char ch; ch = ‘A’; putchar(ch); }

  11. ผลการรัน C Language Easy and Fun การแสดงผลลัพธ์ การแสดงผลข้อความด้วยฟังก์ชัน puts() รูปแบบ:puts(string) เมื่อstring คือตัวแปรที่เก็บข้อความหรือข้อความที่อยู่ใต้เครื่องหมาย “ ” ตัวอย่าง #include <stdio.h> char message[ ] = “C Language”; void main ( ) { puts (message); puts (“Easy and Fun”); }

  12. ตัวอย่างโปรแกรม #include<stdio.h> char x [30]; void main ( ) { printf (“Enter your name :”); gets(x); printf (“Your name :%s\n”, x); } ผลลัพธ์ Enter your name : maneedee Your name : maneedee

  13. การกำหนดค่าให้กับตัวแปรเก็บข้อความการกำหนดค่าให้กับตัวแปรเก็บข้อความ Example #include<stdio.h> void main ( ) { char mass[11] = “C Language”; char book[4] = {‘A’, ‘B’, ‘C’, ‘\0’}; printf (“%s\n”, mass); printf (“%s\n”, book); printf (“%c\n”, mass[3]); } ผลการรัน C Language ABC a

  14. ฟังก์ชันที่ใช้กับข้อความและอักขระฟังก์ชันที่ใช้กับข้อความและอักขระ #include <string.h>

  15. ฟังก์ชันที่ใช้กับข้อความและอักขระฟังก์ชันที่ใช้กับข้อความและอักขระ #include <ctype.h>

  16. #include<stdio.h> #include<string.h> int main() { char name1[10], name2[10], temp[10]; int res; printf("enter 2 string : "); scanf("%s %s",name1,name2); // cannot use like this //name1 = "dararat"; res = strcmp(name1,name2); if (res>0) { printf("greater\n"); strcpy(temp,name1); strcpy(name1,name2); strcpy(name2,temp); printf("%s %s\n",name1,name2); } else if (res<0) printf("less\n"); else // res == 0 { printf("same and length = "); printf("%d\n",strlen(name1)); } return 0; } ตัวอย่างโปรแกรมการใช้ฟังก์ชันของ string

  17. Don’t Sleep Yet…

  18. String Processing • Pattern Matching • Encryption/Decryption • Compression

  19. Pattern Matching • given a string of n characters called text (T) ,and a string of m (m<=n) characters called pattern (P) • find a substring of the text that match the pattern

  20. Pattern Matching Algorithms • Brute-Force • Boyer-Moore • Knuth-Morris-Pratt

  21. Pattern Matching Algorithms Input: An array T[n] of n characters representing a text and an array P[m] of m character representing a patterm Output: The index of the first character in the text that starts a matching substring or -1 if the search is unsuccessful

  22. Brute-Force algorithm Algorithm for i to n-mdo j 0 while j< m and P[j] = T[i+j] do j j+1 ifj = mreturni return -1

  23. Brute-Force algorithm Example 1 T = “abacaabaccabacababb” P = “abacab” abacaabaccabacababb abacab abacab abacab abacab …. abacab Result = “match” 27 comparisons

  24. Brute-Force algorithm Example 2 T = “a pattern matching algorithm” P = “algor” apattern matching algorithm algor algor algor algor … algor Result = “match” ? comparisons

  25. Boyer-Moore algorithm Algorithm • create a lookup table from P • เช่นT = “abacaabadcabacababb” P = “abacab” ***Last(cha) = last position of the character in P

  26. Boyer-Moore algorithm Algorithm (ต่อ) • compare from right to left 1-by-1 • if not equal at position i in T, • if Ti is in lookup table • n = |last(Ti)-last(Pi)| • shift comparison to the next n location of T • if Ti is not in lookup table • shift comparison to the next m (ex. m=6) location of T

  27. Boyer-Moore algorithm Example 1 T = “abacaabadcabacababb” P = “abacab” (m = 6) -------------------------------------------------------------------------- abacaabadcabacababb abacab abacab abacab abacab abacab abacab 13 comparisons

  28. Boyer-Moore algorithm Example 2 T = “a pattern matching algorithm” P = “algor” (m = 5) ------------------------------------------------------------------ a pattern matching algorithm algor algor algor algor algor ? comparisons

  29. Boyer-Moore algorithm แบบฝึกหัด T = “a pattern matching algorithm” P = “rithm” (m = 5) -------------------------------------------------------------------------

  30. Encryption/Decryption • Cryptographic computations encryption/decryption , digital signatures • To support secure communication over the Internet –using cryptographic computations for information security services

  31. Encryption/Decryption • Before sending text, the original text – called plaintext , is encrypted into an unrecognizable string – called ciphertext • After receiving the ciphertext, it is decrypted back to the plaintext encryption Plaintext Ciphertext decryption

  32. Encryption/Decryption • Basic techniques : Substitution , Transposition , Bit manipulation • the essential thing for each method is the secret key • Well-known Algorithms • DES (Data Encryption Standard) • RSA (Rivest-Skamir-Adleman) • Knapsack

  33. Encryption/Decryption Example • Substitution (n = 4) • Transposition (3x6) • Bit manipulation (b = 3)

  34. Compression • given a string X (ascii, unicode), encode into a small binary string Y • Huffman coding algorithm – based on character frequency to construct a binary tree , uses greedy method

  35. Compression Eample “a fast runner need never be afraid of the dark” • frequency of each character

  36. Compression a = ‘010’ r = ‘011’ e = ‘100’ d = ‘1010’ 46 Eample (ต่อ) • Huffman tree 0 1 27 19 1 0 0 1 12 15 10 1 0 1 0 1 9 5 e … a r 0 1 7 d 5 5 … 3

  37. การบ้าน • Knuth-Morris-Pratt algorithm

More Related