Character Codes
The ASCII character code:
| 0 |
NUL |
1 |
SOH |
2 |
STX |
3 |
ETX |
4 |
EOT |
5 |
ENQ |
6 |
ACK |
7 |
BEL |
| 8 |
BS |
9 |
HT |
10 |
NL |
11 |
VT |
12 |
NP |
13 |
CR |
14 |
SO |
15 |
SI |
| 16 |
DLE |
17 |
DC1 |
18 |
DC2 |
19 |
DC3 |
20 |
DC4 |
21 |
NAK |
22 |
SYN |
23 |
ETB |
| 24 |
CAN |
25 |
EM |
26 |
SUB |
27 |
ESC |
28 |
FS |
29 |
GS |
30 |
RS |
31 |
US |
| 32 |
SP |
33 |
! |
34 |
" |
35 |
# |
36 |
$ |
37 |
% |
38 |
& |
39 |
' |
| 40 |
( |
41 |
) |
42 |
* |
43 |
+ |
44 |
, |
45 |
- |
46 |
. |
47 |
/ |
| 40 |
0 |
49 |
1 |
50 |
2 |
51 |
3 |
52 |
4 |
53 |
5 |
54 |
6 |
55 |
7 |
| 56 |
8 |
57 |
9 |
58 |
: |
59 |
; |
60 |
< |
61 |
= |
62 |
> |
63 |
? |
| 64 |
@ |
65 |
A |
66 |
B |
67 |
C |
68 |
D |
69 |
E |
70 |
F |
71 |
G |
| 72 |
H |
73 |
I |
74 |
J |
75 |
K |
76 |
L |
77 |
M |
78 |
N |
79 |
O |
| 80 |
P |
81 |
Q |
82 |
R |
83 |
S |
84 |
T |
85 |
U |
86 |
V |
87 |
W |
| 88 |
X |
89 |
Y |
90 |
Z |
91 |
[ |
92 |
/ |
93 |
] |
94 |
- |
95 |
_ |
| 96 |
` |
97 |
a |
98 |
b |
99 |
c |
100 |
d |
101 |
e |
102 |
f |
103 |
g |
| 104 |
h |
105 |
i |
106 |
j |
107 |
k |
108 |
l |
109 |
m |
110 |
n |
111 |
o |
| 112 |
p |
113 |
q |
114 |
r |
115 |
s |
116 |
t |
117 |
u |
118 |
v |
119 |
w |
| 120 |
x |
121 |
y |
122 |
z |
123 |
{ |
124 |
- |
125 |
} |
126 |
~ |
127 |
DEL |
所有不可印出的字元最高三bits為0或是最低七bits為1。這項性質可用來在印出垃圾前消除這些不可印出的字元。
大寫和小寫字母都是連續的,因此我們可用迴圈來掃過a-z或是A-Z。
由於上面性質,我們可以把一個字元轉換成對應的類別。例如"I"就可以直接減去"A"而得到8,同理"A"減去"A"得到0。
同樣的,我們可以利用上面性質把一個字元從大寫轉換成小寫。例如"c" = "C" - "A" + "a"。
UNIX和Windows系統的換行不太一樣。UNIX的換行只有單純的新列字元10 (New Line, ^J),而Windows的換行則是由歸位字元13 (Carriage Return, ^M)及新列字元10 (New Line, ^J)所組成。所以在UNIX上用vi來看Windows/DOS的文字檔行尾會有^M。
| 平台 |
換行 |
| UNIX |
10 (^J) |
| Windows |
13 10 (^M^J) |
ISO Latin-1是最重要的8-bit code,包含了所有歐洲語系的字元。
許多其他種code像是Unicode使用2 ~ 3 bytes去表示一個symbol,然而ASCII仍然存在其中,這時我們可以利用最高bit是否為0來判斷這是單一byte的字元或是雙byte的字元。
較老的語言像是Pascal, C, C++都視char型態為8-bit。而Java則是為了支援Unicode而視char型態為16-bit,這時ASCII/ISO Latin 1的前八bits都為0。
[Top]
Representing Strings
小整理:
| Representation |
Language |
Explanation |
| Null-terminated Arrays |
C/C++ |
字串視為字元陣列,結尾以NULL字元"\0"也就是ASCII的0結束。 |
| Array Plus Length |
Java |
字元陣列的第一個空間先存字串的長度,而不需要任何結尾符號。 |
| Linked Lists of Characters |
- |
字串使用linked list來連結,如此會產生很多指標佔據空間,比較適用於經常性增加/移除中間子字串的時候。 |
[Top]
Program Design Example: Corporate Renamings
5
"jasonmel" to "榕樹"
"starryalley" to "月亮"
"redjava" to "小蛙"
"mobo" to "淫毛"
"lym" to "鴨子"
3
今天早上 jasonmel 跟 starryalley 看到 redjava 載著鎮妹去吃早餐,
於是就去跟 mobo 和 lym 說,
不過當場逮到 mobo 在和 moboGirl 打桌球。
Output:
今天早上 榕樹 跟 月亮 看到 小蛙 載著鎮妹去吃早餐,
於是就去跟 淫毛 和 鴨子 說,
不過當場逮到 淫毛 在和 淫毛Girl 打桌球。
Code前面的宣告部份:
#include <string.h>
#define MAXLEN 1001 /* longest possible string */
#define MAXCHANGES 101 /* maximum number of name changes */
typedef char string[MAXLEN];
string mergers[MAXCHANGES][2]; /* store before/after corporate names */
int mergers; /* number of different name changes */
讀取改變的資料:
read_changes()
{
int i;
scanf("%d\n", &nmergers); /* 有幾筆資料 */
for (i = 0; i < nmergers; i++) {
read_quoted_string(&(mergers[i][0]));
read_quoted_string(&(mergers[i][1]));
}
}
read_quoted_string(char *s)
{
int i = 0; /* counter */
char c; /* latest character */
while ((c = getchar()) != '\"') ;
while ((c = getchar()) != '\"\) {
s[i] = c;
i = i + 1;
}
s[i] = '\0';
}
[Top]
Searching for Patterns
Return the position of the first occurrence of the pattern p in the text t, and -1 if it does not occur.
int findmatch(char *p, char *t)
{
int i, j; /* counters */
int plen, tlen; /* string lengths */
plen = strlen(p);
tlen = strlen(t);
for (i = 0; i <= (tlen - plen); i = i + 1) {
j = 0;
while ((j < plen) && (t[i + j] == p[j]))
j = j + 1;
if (j = plen) return(i);
}
return(-1);
}
This algorithm take as much as O(|p||t|) time in the worst case.
[Top]
Manipulating Strings
把字串看成陣列使得以下工作變的很簡單:
計算字串長度-掃描整個字串同時增加計數器直到碰到結束符號。
複製字串-一個字元一個字元複製到新字串,別忘了結束符號及預留足夠的空間。
反轉字串-最簡單的方法就是由右到左拷貝字元到新字串。如果允許摧毀原來的字串的話也可以考慮把左右對應的字元swap。
Replace the substring of length xlen starting at position pos in string s with the contents of string y.
replace_x_with_y(char *s, int pos, int xlen, char *y)
{
int i; /* counter */
int slen, ylen; /* lengths of relevant strings */
slen = strlen(s);
ylen = strlen(y);
if (xlen >=ylen) // 往前搬
for (i=(pos+xlen); i<=slen; i++) s[i+(ylen-xlen)] = s[i];
else // 往後搬
for (i=slen; i<=(pos+xlen); i--) s[i+(ylen-xlen)] = s[i];
// 把y搬進x的位置
for (i = 0; i < ylen; i++) s[pos + i] = y[i];
}
[Top]
Completing the Merger
main()
{
string s; /* input string */
char c; /* input character */
int nlines; /* number of lines in text */
int i, j; /* counters */
int pos; /* position of pattern in string */
read_changes();
scanf("%d\n", &nlines);
for (i = 1; i <= nlines; i = i + 1) { /* read text line */
j = 0;
wihle ((c = getchar()) != '\n') {
s[j] = c;
j = j + 1;
}
s[j] = '\0';
for (j = 0; j < nmergers; j = j + 1)
while ((pos = findmatch(mergers[j][0], s)) != -1) {
replace_x_with_y(s, pos,
strlen(mergers[j][0]), mergers[j][1]);
}
printf("%s\n", s);
}
}
[Top]
String Library Functions
C Library String Functions
#include <ctype.h> /* include the character library */
int isalpha(int c); /* true if c is either upper or lower case */
int isupper(int c); /* true if c is upper case */
int islower(int c); /* true if c is lower case */
int isdigit(int c); /* true if c is a numerical digit (0-9) */
int ispunct(int c); /* true if c is a punctuation(標點符號) symbol */
int isxdigit(int c); /* true if c is a hexadecimal digit (0-9, A-F) */
int isprint(int c); /* true if c is any printable character */
int toupper(int c); /* convert c to upper case case -- no error checking */
int tolower(int c); /* convert c to lower case case -- no error checking */
#include <string.h> /* include the string library */
char *strcat(char *dst, const char *src); /* concatenation: dst = dst + src */
int strcmp(const char *s1, const char *s2); /* is s1 == s2? */
char *strcpy(char *dst, const char *src); /* copy src to dist */
size_t strlen(const char *s); /* length of string */
char *strstr(const char *s1, const char *s2); /* search for s2 in s1, 回傳s1在s2中的位址 */
char *strtok(char *s1, const char *s2); /* iterate words in s1 */
C++ String Library Functions
string::size() /* string length */
string::empty() /* is it empty */
string::c_str() /* return a pointer to a C style sting */
string::operator [](size_type i) /* access the ith character */
string::append(s) /* append to string */
string erase(n,m) /* delete a run of characters */
string::insert(size_type n, const string&s) /*insert string s at n */
string::find(s)
string::rfind(s) /* search left to right for the given string */
string::first()
string::last() /* get characters, also there are iterators */
Java String Objects
The String class is for static strings which do not change, while StringBuffer is designed for dynamic strings. The java.text package contains more advanced operations on strings.
[Top]