Upload
requiemformemories
View
665
Download
5
Embed Size (px)
Citation preview
CH4 字元陣列認識許多函式好朋友 !?
程教 文月
憶.資料型別★ 還記得資料型別嗎?除了 int 以外似乎還有
double 、 float…… 等等
★ 其中有一種名為 char 的東西!
char 是什麼呢?讓我們繼續看下去 >w<
char 的宣告與初始化• 沒錯! char 正是個字元喔!• 宣告方式像這樣: char name;
• 如果要初始化的話: char name='a';
• 有沒有非常簡單呢?
char 陣列!★ 這樣一個 char 只能存一個字母好不方便…
…不如來宣告個 char 陣列吧!
★ 形式如下: char name[10];
• 如果要初始化的話: char name[10]="Ted";
空字元• 考考你,宣告一個字元陣列存入 "hello" ,這個字
元陣列的長度是多少呢?答案不是 4 ,是 5 !• 字元陣列的最後一個元素是 '\0' ,表示字元陣列
的結束。
h e l l o \0 …
神秘的 ascii 碼試著這樣寫,看看會發生甚麼事:char x=65;
cout<<x<<endl;
注意不要寫成 "65" 喔
哇喔!居然是 A 耶 >w<
到底發生什麼事情了呢?執行結果
神秘的 ascii 碼• C++ 中字元相當於是常數,每個字都有其
對應的數值,此對應表稱為 ASCII 表,例如: A 相當於 65 。
那其他的字元的常數是什麼呢?
讓我們來看看 ascii 碼表!
神秘的 ascii 碼
ctype 好朋友們★ 使用前引入標頭檔 <cctype> ( 是回傳 1 ,不是回傳
0)isprint(char) 是否為可列印字元 (ASCII 碼 32~126)(ASCII碼 32為 space)
isgraph(char)
是否為可顯示字元 (ASCII 碼 33~126)
isalnum(char)
是否為英文字母或數字字元 (A~Z, a~z, 0~9)
isalpha(char) 是否為英文字母 (A~Z, a~z)
isdigit(char) 是否為數字字元 (0~9)
isxdigit(char) 是否為 16進位數字字元 (0~9, A~F)
islower(char) 是否為小寫英文字母 (a~z)
isupper(char) 是否為大寫英文字母 (A~Z)
isspace(char) 是否為空白字元 (ASCII 碼 32)
ispunct(char) 是否為標點符號字元,為 print字元但不包含 space、 letter與數字
iscntrl(char) 是否為控制字元, (ASCII碼 0-31) 與 DEL(0x7F)
tolower(char) 轉成小寫英文字母
toupper(char)
轉成大寫英文字
cstring 好朋友們★ 使用前引入標頭檔 <cstring>
int strcmp(const char *string1,const char *string2)比較 string1與 string2相等回傳 0int strlen(const char *string) 回傳 string的長度char * strcpy ( char * destination, const char * source )將 source所指字串複製到 destination所指字串中char * strcat ( char * destination, const char * source )將 source所指字串接到 destination所指字串的後面char *strtok(char *string1, const char *string2)將 string1所指字串使用 string2所指分隔字元,切成幾個字串。
神秘的 string 物件★ 使用前引入標頭檔 <string>
★ 和字元陣列些許相似★ 宣告:string s1;
string s2= "hello";
範例★ 瞬間認識了這麼多好朋友,已經暈頭轉向
了嗎?不要緊,我們來看範例吧!
isalnum(char)示範
★code 如下:#include<iostream>#include<cctype>using namespace std;int main(){
char a[]="zsisc*20";int i=0;while(a[i]!='\0'){
if(isalnum(a[i]))cout<<" 第 "<<i+1<<" 項是英文字母或數字 "<<e
ndl; i++;
}}
- 印出哪些是英文字母或數字
執行結果
大寫轉小寫小寫轉大寫
★code 如下:#include<iostream>#include<cctype>using namespace std;int main(){
char str[]="ZSISC is the best";int i=0;while(str[i]!='\0'){
if(islower(str[i]))str[i]=toupper(str[i]);
elsestr[i]=tolower(str[i]);
i++;}cout<<str<<endl;
}
執行結果
strcmp示範
★code 如下:#include <iostream>#include <cstring>using namespace std;int main(){char s1[]="FumiTsuki";char s2[]="fumitsuki";if (!strcmp(s1,s2))
cout << "s1 等於 s2" <<endl;else
cout << "s1 不等於 s2" <<endl;}
- 比較兩字串相不相等
執行結果
strcpy 示範★code 如下:#include <iostream>#include <cstring>using namespace std;int main(){char s1[]="Fumitsuki";char s2[]="EightPlates";cout<<" 原來字串 s1 為 "<<s1<<endl;strcpy(s1,s2);cout << " 複製後字串 s1 為 " <<s1<<en
dl; }
執行結果
- 複製字串至另一個字串
strcat 示範★code 如下:#include <iostream>#include <cstring>using namespace std;int main(){char s1[]="Fumi";char s2[]="Tsuki";cout << " 原來字串 s1 為 " << s1<<endl;strcat(s1,s2);cout << " 串接後字串 s1 為 " << s1<<end
l; }
- 複製字串至另一個字串
執行結果
strtok 示範★code 如下:#include <iostream>
#include <cstring>
using namespace std;
int main(){
char s1[]="fumi tsuki";
char s2[]=" ";
strtok(s1,s2);
cout<<" 切除後的結果是 :"<<s1<<endl;
}
執行結果
- 字串以空白切開
strlen 示範★code 如下:#include<iostream>#include<cstring>using namespace std;int main(){
char a[]="helloC++"; cout<<" 此字串長度是 "<<strlen(a)<<endl;
}
- 計算字串長度
執行結果
轟炸到此結束謝謝各位的聆聽
記得寫作業才會了解概念喔!