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

Download Report

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

(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);
การแสดงผลลัพธ์
การแสดงผลข้อความด้วยฟังก์ชนั puts()
รูปแบบ:
puts(string)
เมือ่ string คือตัวแปรทีเ่ ก็บข้อความหรือข้อความทีอ่ ยูใ่ ต้เครื่องหมาย “ ”
ตัวอย่าง
#include <stdio.h>
char message[ ] = “C Language”;
void main ( )
{
}
puts (message);
puts (“Easy and Fun”);
ผลการรัน
C Language
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 : manee dee
Your name : manee dee
การกาหนดค่าให้ กบั ตัวแปรเก็บข้ อความ
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>
ชื่อฟังก์ชัน
strcpy( )
strcat( )
รู ปแบบ
strcpy(str1,str2)
strcat(str1,str2)
strlen( )
strcmp()
strlen(s)
strcmp(str1,str2)
ความหมาย
คัดลอกข้อมูลจำก str2 ไปยัง str1
ใช้เชื่อมต่อข้อควำม str1 กับ str2 เข้ำด้วยกัน
และเก็บผลลัพธ์ไว้ใน str1
ใช้หำควำมยำวของข้อควำม s
เปรี ยบเทียบข้อควำม str1 กับ str2
ฟั งก์ชนั ที่ใช้ กบั ข้ อความและอักขระ
#include <ctype.h>
ชื่อฟังก์ชนั
รู ปแบบ
ควำมหมำย
tolower( )
tolower(ch) เปลี่ยนตัวอักษรจำกตัวใหญ่ให้เป็ นตัวเล็ก
toupper( )
toupper(ch) เปลี่ยนตัวอักษรจำกตัวเล็กให้เป็ นตัวใหญ่
ตัวอย่ำงโปรแกรมกำรใช้ฟังก์ชนั ของ string
#include<stdio.h>
#include<string.h>
if (res>0)
{
printf("greater\n");
strcpy(temp,name1);
strcpy(name1,name2);
strcpy(name2,temp);
int main()
{
char name1[10], name2[10], temp[10];
int res;
printf("enter 2 string : ");
scanf("%s %s",name1,name2);
printf("%s %s\n",name1,name2);
}
else if (res<0)
printf("less\n");
else // res == 0
{
// cannot use like this
//name1 = "dararat";
printf("same and length = ");
printf("%d\n",strlen(name1));
}
return 0;
res = strcmp(name1,name2);
}
Don’t Sleep Yet…
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-m do
j 0
while j< m and P[j] = T[i+j] do
j j+1
if j = m return i
return -1
Brute-Force algorithm
Example 1
T = “abacaabaccabacababb”
P = “abacab”
abacaabaccabacababb
abacab
abacab
Result = “match”
abacab
abacab
….
abacab
27 comparisons
Brute-Force algorithm
Example 2
T = “a pattern matching algorithm”
P = “algor”
apattern matching algorithm
algor
algor
a lgor
algor
…
Result = “match”
algor
? comparisons
Boyer-Moore algorithm
Algorithm
 create a lookup table from P
 เช่น T = “abacaabadcabacababb”
P = “abacab”
cha
a
b
c
d
Last(cha)
4
5
3
-1
***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
cha
a
b
c
d
abacab
Last(cha)
4
5
3
-1
abacab
abacab
abacab
13 comparisons
abacab
abacab
Boyer-Moore algorithm
Example 2
T = “a pattern matching algorithm”
P = “algor” (m = 5)
-----------------------------------------------------------------a pattern matching algorithm
algor
algor
algor
algor
algor
cha
a
l
g
o
r
Last(cha)
0
1
2
3
4
? comparisons
Boyer-Moore algorithm
แบบฝึ กหัด
T = “a pattern matching algorithm”
P = “rithm” (m = 5)
------------------------------------------------------------------------cha
Last(cha)
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
Plaintext
encryption
decryption
Ciphertext
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
char. a b d e f h i k n o r s t u v
freq. 9 5 1 3 7 3 1 1 1 4 1 5 1 2 1 1
a = ‘010’
r = ‘011’
e = ‘100’
d = ‘1010’
Compression
Eample (ต่อ)
46
0
 Huffman tree
1
27
19
0
12
10
9
0
a
15
1
0
1
e
r
5
1
5
0
7
5
1
0
1
d
…
1
3
…
การบ้ าน
 Knuth-Morris-Pratt algorithm