![Character字元字符單字_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/e1ed1a6f-72f0-415c-8bee-91911cb72bed/e1ed1a6f-72f0-415c-8bee-91911cb72bed1.gif)
![Character字元字符單字_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/e1ed1a6f-72f0-415c-8bee-91911cb72bed/e1ed1a6f-72f0-415c-8bee-91911cb72bed2.gif)
![Character字元字符單字_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/e1ed1a6f-72f0-415c-8bee-91911cb72bed/e1ed1a6f-72f0-415c-8bee-91911cb72bed3.gif)
![Character字元字符單字_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/e1ed1a6f-72f0-415c-8bee-91911cb72bed/e1ed1a6f-72f0-415c-8bee-91911cb72bed4.gif)
![Character字元字符單字_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/e1ed1a6f-72f0-415c-8bee-91911cb72bed/e1ed1a6f-72f0-415c-8bee-91911cb72bed5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Characters and Strings in CCharacter字元/字符/單字A character constant is represented as a character in single quotes單引號. Example: 'a', '5'The data type for character in C is charTo declare宣告 a character variable變量/變數(shù)char abc;To assign value to a character variableabc = 'A'String字符
2、串/文字A string constant常量 is represented as a string of characters enclosed by two double quotation marks雙引號.Example: "I am happy".You may view a string as a pointer指標(biāo) or an array陣列.· View a string as an array陣列A string in C is actually an array of characters單字陣列.Every string in C is nu
3、ll-terminated with the null character '0' 結(jié)束字符.char st="I am happy"Iamhappy0char st='I',' ','a','m',' ','h','a','p','p','y','0'The size of the st variable will be fixed to 11.To make a reference
4、 to a character in a string is similar to make a reference to an element in an array.st0 = 'i'st2 = 'a'st7 = 'p'· View a string as a pointer指標(biāo)The value of a string is the address地址 of its first character首字元.char *colorPtr = "blue"But you cannot do the following
5、 thing colorPtr1 = 'r'/ 不合法*(colorPtr+1) = 'r'/ 合法as colorPtr points to a string constant and you cannot change part of the constant.Character operations字元運算For character operations, there is a library of character functions to find out if a character is a digit, a lowercase letter,
6、etc.In order to use the functions in the library, you have to add the following line to your C source file.#include <ctype.h>Some of the functions in this library are shown below:Function prototypeFunction descriptionint isdigit(int c)是否數(shù)字Returns a true value if c is a digit, and 0 otherwise.i
7、nt isalpha(int c)是否字母Returns a true value if c is a letter, and 0 isalnum(int c)是否數(shù)字或字母Returns a true value if c is a digit or a letter, and 0 islower(int c)是否小楷字母Returns a true value if c is a lowercase letter, and 0 isupper(int c)是否大楷字母Returns a true value
8、 if c is an uppercase letter, and 0 tolower(int c)轉(zhuǎn)小楷字母If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument toupper(int c)轉(zhuǎn)大楷字母If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper
9、 returns the argument unchanged.String operations文字運算There are several functions to convert strings of digits to integer or floating-point values.Function prototypeFunction descriptiondouble atof(const char *nPtr)文字轉(zhuǎn)小數(shù)Converts the string nPtr to atoi(const char *nPtr)文字轉(zhuǎn)整數(shù)Converts the str
10、ing nPtr to int.long atol(const char *nPtr)文字轉(zhuǎn)長整數(shù)Converts the string nPtr to long int.For example,atof("1.234") will return the floating-point number 1.234. atoi("532") will return the integer number 532.For more string operations, there is a library of string functions for compa
11、ring strings, searching strings, etc.In order to use the functions in that library, you have to add the following line to your C source file.#include <string.h>Some of the functions in this library are shown below:· Find out the length of a string字長/字數(shù)The length of a string does not inclu
12、de the '0' character.Function prototypeFunction descriptionsize_t strlen(const char *s)字長/字數(shù)Determines the length of string s. The number of characters preceding the terminating '0' character is returned.For example,strlen("ABCDEFG") will return 7.Another example,char temp1
13、9 = "CSC2100b"strlen(temp) will return 8.· Copying strings複製文字In C, you have to use the following two functions for copying strings.Function prototypeFunction descriptionchar *strcpy(char *s1, const char *s2)複製全部Copies the string s2 into the array s1. The value of s1 is returned (s1
14、172;s2)char *strncpy(char *s1, const char *s2, size_t n)複製部份Copies at most n characters of the string s2 into the array s1. The value of s1 is returnedFor example, to copy the whole string複製全部from s2 to s1 (s1¬s2)strcpy(s1, s2);For example, to copy only the first 3 characters複製部份(首3字元)in s2 to
15、s1strncpy(s1, s2, 3);As mentioned before, strings in C are simply array of characters. You need to be careful on the size of the string小心字數(shù)/長度.For example,char temp14;char temperror3;char temp2 = "ABC"char temp3100;strcpy(temp1, temp2);/* temp1 = temp2 = "ABC" */strcpy(temperror,
16、 temp2);/* 沒有 '0' */strcpy(temp3, temp2);/* temp3 = temp2 = "ABC" */· Concatenating strings文字合併In C, you have to use the following two functions for concatenating strings.Function prototypeFunction descriptionchar *strcat (char *s1, const char *s2)(s1¬s1+s2) 全部合併Appends t
17、he string s2 to the array s1. The first character of s2 overwrites the terminating '0' character of s1. The value of s1 is returned.char *strncat (char *s1, const char *s2, size_t n)(s1¬s1+s2) 部份合併Appends at most n characters of string s2 to array s1. The first character of s2 overwrite
18、s the terminating '0' character of s1. The value of s1 is returned.For example, char s1100 = "Happy "char s2100 = "Happy "char s3 = "Chinese New Year!"strcat(s1, s3); /* s1 become "Happy Chinese New Year!"*/strncat(s2, s3, 7);s213 = '0' /* s2 b
19、ecome "Happy Chinese" */· Comparing strings比較文字In C, you have to use the following two functions for comparing two strings.Function prototypeFunction descriptionint strcmp(const char *s1, const char *s2)比較全部 (s1-s2)Compares the string s1 to the string s2. The function returns 0, less
20、than 0, or greater than 0 if s1 is equal to, less than, or greater than s2, strncmp(const char *s1, const char *s2, size_t n)比較部份Compares up to n characters of the string s1 to the string s2. The function returns 0, less than 0, or greater than 0 if s1 is equal to, less than, or gre
21、ater than s2, respectively.For example,char *s1 = "Happy New Year"/ N=78char *s2 = "Happy New Year"char *s3 = "Happy Birthday"/ B=66n=strcmp(s1, s2);/* 0 (s1=s2) */n=strcmp(s1, s3);/* +1 (s1>s3) */n=strcmp(s3, s1);/* 1 (s3<s1) */n=strncmp(s1, s3, 6);/* 0 "Hap
22、py " */n=strncmp(s1, s3, 7);/* +1 "Happy N" > "Happy B" */n=strncmp(s3, s1, 7);/* 1 "Happy B" < "Happy N" */· Searching找尋/檢索In C, you may use the following functions for searching strings for characters and other strings.Function prototypeFunct
23、ion descriptionchar *strchr(const char *s, int c)文字s中找字元cLocates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise a NULL pointer is returned.char *strstr(const char *s1, const char *s2)文字s1中找文字s2Locates the first occurrence in string s1 of st
24、ring s2. If the string is found, a pointer to the string in s1 is returned. Otherwise a NULL pointer is returned.char *strtok(char *s1, const char *s2)打散文字s1A sequence of calls to strtok breaks string s1 into "tokens" logical pieces such as words in a line of text separated by characters c
25、ontained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.For example
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 暑期教師培訓(xùn)學(xué)習(xí)計劃
- 2025年度家庭裝修石材加工及安裝服務(wù)合同范本
- 專業(yè)性合同范本
- 辦公裝飾合同范本
- 買賣攤位合同范本
- 2025年度生態(tài)農(nóng)業(yè)項目土地流轉(zhuǎn)合作合同
- 公司家具購買合同范本
- 會議營銷分成合同范本
- 關(guān)于解除兼職合同范本
- 法律盡職調(diào)查報告模板
- 胸腔積液護理查房-范本模板
- 水土保持方案中沉沙池的布設(shè)技術(shù)
- 安全生產(chǎn)技術(shù)規(guī)范 第25部分:城鎮(zhèn)天然氣經(jīng)營企業(yè)DB50-T 867.25-2021
- 現(xiàn)代企業(yè)管理 (全套完整課件)
- 走進本土項目化設(shè)計-讀《PBL項目化學(xué)習(xí)設(shè)計》有感
- 《網(wǎng)店運營與管理》整本書電子教案全套教學(xué)教案
- 教師信息技術(shù)能力提升培訓(xùn)課件希沃的課件
- 高端公寓住宅項目營銷策劃方案(項目定位 發(fā)展建議)
- 執(zhí)業(yè)獸醫(yī)師聘用協(xié)議(合同)書
- 第1本書出體旅程journeys out of the body精教版2003版
- 2022年肝動脈化療栓塞術(shù)(TACE)
評論
0/150
提交評論