370 likes | 545 Views
การประมวลผลสายอักขระ. (String Processing). Outline. ทบทวน String การประมวลผลสายอักขระ (String Processing) Pattern Matching Encryption/Decryption Compression. ทบทวน String. การประกาศตัวแปรเพื่อเก็บข้อความ การรับและการแสดงผลข้อความ การกำหนดค่าให้กับตัวแปรเก็บข้อความ
E N D
การประมวลผลสายอักขระ (String Processing)
Outline • ทบทวน String • การประมวลผลสายอักขระ (String Processing) • Pattern Matching • Encryption/Decryption • Compression
ทบทวน String • การประกาศตัวแปรเพื่อเก็บข้อความ • การรับและการแสดงผลข้อความ • การกำหนดค่าให้กับตัวแปรเก็บข้อความ • ฟังก์ชันที่ใช้กับข้อความและอักขระ
การประกาศตัวแปรเพื่อเก็บข้อความการประกาศตัวแปรเพื่อเก็บข้อความ • รูปแบบ char ชื่อตัวแปร[จำนวนอักขระ] โดยที่จำนวนอักขระต้องมากกว่าจำนวนอักขระที่เก็บจริง 1 ช่อง เพราะช่องสุดท้ายต้องเก็บอักขระ NULL ซึ่งเขียนแทนด้วย‘\0’ เพื่อบอกให้ตัวแปลภาษารู้ว่าเป็นข้อความ • ตัวอย่าง char name[10]; หมายถึง ตัวแปรชื่อ name เก็บข้อความยาว 9 อักขระ char color[ ]; หมายถึง ตัวแปรชื่อ color เก็บข้อความโดยไม่กำหนดขนาด ซึ่งในกรณีนี้ตัวแปลภาษาจะกำหนดขนาดให้เท่ากับจำนวนอักขระบวก 1
การรับข้อมูล การรับข้อความโดยใช้ฟังก์ชัน scanf() • รูปแบบ:scanf (“%s”, ชื่อตัวแปร) • ตัวอย่าง #include <stdio.h> char massage[ 20]; void main ( ) { scanf (“%s”, message); printf (“%s”, message); }
การรับข้อมูล รับข้อมูลทีละตัวอักษรด้วยฟังก์ชัน getchar ( ) • การทำงาน>>เมื่อผู้ใช้กรอกตัวอักษรแล้ว จะต้องกดปุ่ม Enter โปรแกรมจึงจะกลับไปทำงานต่อ โดยอักขระที่ผู้ใช้กรอก จะปรากฏขึ้นมาให้เห็นบนหน้าจอด้วย • รูปแบบ: ชื่อตัวแปร = getchar() • ตัวอย่าง #include <stdio.h> void main ( ) { char ch; ch = getchar(); printf("You type a character is ...%c \n",ch); }
การรับข้อมูล รับข้อความด้วยฟังก์ชัน gets ( ) • รูปแบบ:gets(ชื่อตัวแปร) • ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str); printf("You type a string is ...%s \n",str); }
การแสดงผลลัพธ์ การแสดงผลข้อความโดยใช้ฟังก์ชัน printf • รูปแบบ: printf (“%s”, ชื่อตัวแปร); • ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str); printf("You type a string is ...%s \n",str); }
การแสดงผลลัพธ์ การแสดงผลทีละตัวอักษรโดยใช้ฟังก์ชัน 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] ); }
การแสดงผลลัพธ์ การแสดงผลทีละตัวอักษรด้วยฟังก์ชัน putchar() รูปแบบ:putchar (argument) argument : ตัวแปร, ค่าคงที่ , ฟังก์ชัน ตัวอย่าง #include <stdio.h> void main ( ) { char ch; ch = ‘A’; putchar(ch); }
ผลการรัน C Language Easy and Fun การแสดงผลลัพธ์ การแสดงผลข้อความด้วยฟังก์ชัน puts() รูปแบบ:puts(string) เมื่อstring คือตัวแปรที่เก็บข้อความหรือข้อความที่อยู่ใต้เครื่องหมาย “ ” ตัวอย่าง #include <stdio.h> char message[ ] = “C Language”; void main ( ) { puts (message); puts (“Easy and Fun”); }
ตัวอย่างโปรแกรม #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
การกำหนดค่าให้กับตัวแปรเก็บข้อความการกำหนดค่าให้กับตัวแปรเก็บข้อความ 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
ฟังก์ชันที่ใช้กับข้อความและอักขระฟังก์ชันที่ใช้กับข้อความและอักขระ #include <string.h>
ฟังก์ชันที่ใช้กับข้อความและอักขระฟังก์ชันที่ใช้กับข้อความและอักขระ #include <ctype.h>
#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
String Processing • Pattern Matching • Encryption/Decryption • Compression
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
Pattern Matching Algorithms • Brute-Force • Boyer-Moore • Knuth-Morris-Pratt
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
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
Brute-Force algorithm Example 1 T = “abacaabaccabacababb” P = “abacab” abacaabaccabacababb abacab abacab abacab abacab …. abacab Result = “match” 27 comparisons
Brute-Force algorithm Example 2 T = “a pattern matching algorithm” P = “algor” apattern matching algorithm algor algor algor algor … algor Result = “match” ? comparisons
Boyer-Moore algorithm Algorithm • create a lookup table from P • เช่นT = “abacaabadcabacababb” P = “abacab” ***Last(cha) = last position of the character in P
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
Boyer-Moore algorithm Example 1 T = “abacaabadcabacababb” P = “abacab” (m = 6) -------------------------------------------------------------------------- abacaabadcabacababb abacab abacab abacab abacab abacab abacab 13 comparisons
Boyer-Moore algorithm Example 2 T = “a pattern matching algorithm” P = “algor” (m = 5) ------------------------------------------------------------------ a pattern matching algorithm algor algor algor algor algor ? comparisons
Boyer-Moore algorithm แบบฝึกหัด T = “a pattern matching algorithm” P = “rithm” (m = 5) -------------------------------------------------------------------------
Encryption/Decryption • Cryptographic computations encryption/decryption , digital signatures • To support secure communication over the Internet –using cryptographic computations for information security services
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
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
Encryption/Decryption Example • Substitution (n = 4) • Transposition (3x6) • Bit manipulation (b = 3)
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
Compression Eample “a fast runner need never be afraid of the dark” • frequency of each character
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
การบ้าน • Knuth-Morris-Pratt algorithm