# 字元 & 字串

Arvin Liu @ Sprout

# 那 !@#\$ 這些?

## ASCII Code

zh.wikipedia.org/wiki/ASCII

# 告訴cout型態就好！

``````char c1 = 97;
std::cout << c1 << std::endl;``````
``a``

# 你真的記得起ASCII？

``````int c1 = 'a';
std::cout << c1 << std::endl;
``````
``97``
``````char c1 = 'a';
std::cout << c1 << std::endl;
``````

# 跳脫字元

``````char c1 = '
';
std::cout << c1 << std::endl;
``````

# No no no

``````char c1 = 10;
std::cout << c1 << std::endl;
``````

# C/C++/Py... lang 是以 \ 開頭。

(escape character)

# 很難表示的字有很多嘛？ ' 怎麼打?

(escape character)

``'''``

# *會被當字元

``'a'``

' '\''
\ '\\'
" '\"'

'\r'

# char vs int

## char 本身就是個數字。

• +-*/%^ 等常用數字運算符號都可以。
• 範圍是 -128 ~ 127 (因為char是1個byte or 8個bit)

## char 本身就是個數字。

• 'b' == 'a' + 1
• 'A' == 'a' ^ ' '
• 'a' == 'A' ^ ' '
• 3 == 'd' - 'a'
• 'a' == ('z'-'a' + 'b'-'a') % 26 + 'a'

## char 的小提醒

• 範圍很小，不要用來紀錄一些數值的東西。
• 只能是一個字。
• 那要輸出一串字呢？

# Practice!

``````#include <iostream>
using namespace std;
int main(){
char c;
while( cin >> c ){
// 做你的事情
}
}
``````

# char 的 cin

## Solution - A

``````#include <iostream>
using namespace std;
int main(){
char c;
while( cin >> c ){
char v = c + 3;
if ( v > 'z' )
v -= 26;
cout <<  v;
}
cout << '\n';
}
``````

## Solution - B

``````#include <iostream>
using namespace std;
int main(){
char c;
while( cin >> c ){
char v = ((c - 'a') + 3) % 26 + 'a';
cout <<  v;
}
cout << '\n';
}
``````

--> 陣列 !

# When char meets array...

``````char S[] = {'a','p','p','l','e','\0'};
std::cout << S << std::endl;
``````
``apple``

# 字元陣列的表示方法 - ""

``````char S[] = {'a','p','p','l','e',0};
std::cout << S << std::endl;
``````
``````char S[] = "apple";
std::cout << S << std::endl;
``````

==

# 字元陣列的表示方法

``````char S[] = {'a','\n','p','\t','e',0};
std::cout << S << std::endl;
``````
``````char S[] = "a\np\te";
std::cout << S << std::endl;
``````

==

# 字元陣列的Input

``````char S[100];
std::cin >> S;``````

# Example

``````char S[100];
while(cin >> S)
cout << "I get: " << S << endl;
``````
``````Input:
abc def
ghi``````
``````Output:
I get: abc
I get: def
I get: ghi
``````

# Example

``````char S[100];
cin >> S;
cout << strlen(S);``````
``````Input:
abcdef``````
``````Output:
6``````

# Example

``````char S[100];
cin >> S;
cout << (strcmp(S,"QAQ") == 0);``````
``````Input:
abcdef
-------
QAQ``````
``````Output:
1
----
0``````

# Example

``````char A[100], B[100];
cin >> B;
strcpy(A, B);
cout << A;``````
``````Input:
abcdef``````
``````Output:
abcdef``````

# char array tips

(Optional)

``````char A[8] = {'a','b'};
A = {'c' , 'd'};``````

# [] Assignment problem

``````char B[8] = "ab";
B = "cd"; if(A == B){}``````

``````char A[8]="12345678";
char B[8]="Peipei";
std::cout << A << std::endl;
``````

# 我也不知道。

## B的理由？

``char A[8]="12345678";``
``char B[8]="Peipei";``
1 2 3 4 5 6 7 8 '\0'

A

1 2 3 4 5 6 7 8

A

B

P e i P e i '\0'

# Review

## <cstring>

• strlen(S) : 回傳S的長度。(由第一個\0來決定長度)。
• strcmp(A,B) : 回傳0表示A和B一樣。
• strcpy(A, B) : 把B字串複製到A身上。

## char

• 所有字都是數字，印出數字或文字差別在於int還是char。
• 跳脫符號 -> 一有打不出來的字，可以查跳脫符號表。

## string (char array)

• 宣告: 用char陣列。可以用"..."代替{'.', '.', '.'}。
• 除了宣告以外，不能直接用a = "..." 或 a == b等等。
• 結尾是用 '\0' 來表示的，所以空間會比字串多一個字。

> 宣告要多一點空間。

ㄚ不是輸出就不能改了？

' '\''
\ '\\'
" '\"'

'\r'

# 回車？倒退？

``````char S[] = "appleeee\rbanana";
std::cout << S << std::endl;
// bananaee``````
``````char S[] = "0123\baaaa";
std::cout << S << std::endl;
// 012aaaa``````

# How 2 Sleep?

``````#include <windows.h>
int main(){
Sleep(3000);
// 睡3000ms。
}``````

windows

``````#include <unistd.h>
int main(){
usleep(3000);
// 睡3000ms。
}``````

# How 2 Animate?

``````#include <windows.h>
using namespace std;
int main(){
char A[] = "ABCD    ";
char B[] = " ABCD   ";
cout << A ;
Sleep(3000);
cout << '\r';
cout << B ;
}``````

# Solution

``````#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;
int main(){
char S[] = "OAO - QAQ                                     ";
int slen = strlen(S), starting = 0;
while(true){
cout << '\r';
for(int i=starting, j=0 ; j<slen ; i++,j++ ){
cout << S[(i)%slen];
}
starting ++;
Sleep(100);
}
}``````

By Arvin Liu

# 字元 & 字串

Teaching slide - char & char array

• 1,492