




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Characters and Strings in CCharacter字元/字符/單字A character constant is represented as a character in single quotes單引號(hào). 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雙引號(hào).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字元運(yùn)算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文字運(yùn)算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)長(zhǎng)整數(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字長(zhǎng)/字?jǐn)?shù)The length of a string does not inclu
12、de the '0' character.Function prototypeFunction descriptionsize_t strlen(const char *s)字長(zhǎng)/字?jǐn)?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小心字?jǐn)?shù)/長(zhǎng)度.For example,char temp14;char temperror3;char temp2 = "ABC"char temp3100;strcpy(temp1, temp2);/* temp1 = temp2 = "ABC" */strcpy(temperror,
16、 temp2);/* 沒(méi)有 '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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東管理學(xué)院《中國(guó)工藝美術(shù)史》2023-2024學(xué)年第二學(xué)期期末試卷
- 昭通市永善縣2024-2025學(xué)年數(shù)學(xué)三下期末質(zhì)量檢測(cè)模擬試題含解析
- 武漢交通職業(yè)學(xué)院《生物學(xué)課程標(biāo)準(zhǔn)與教材研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 襄陽(yáng)職業(yè)技術(shù)學(xué)院《專業(yè)英語(yǔ)(水文與水資源)》2023-2024學(xué)年第一學(xué)期期末試卷
- 長(zhǎng)春師范大學(xué)《應(yīng)用生物技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 湖北省咸寧市崇陽(yáng)縣2025屆初三年級(jí)元月調(diào)研考試英語(yǔ)試題含答案
- 灌溉工程建設(shè)的生態(tài)環(huán)境保護(hù)考核試卷
- 數(shù)字化醫(yī)療在未來(lái)醫(yī)療中的角色考核試卷
- 木制容器倉(cāng)儲(chǔ)與物流考核試卷
- 畜禽繁殖性能檢測(cè)儀器考核試卷
- 職業(yè)信息與培訓(xùn)項(xiàng)目(專業(yè))對(duì)應(yīng)指引
- 《病理學(xué)》肝硬化課件
- 漢字的五行屬性與三才五格計(jì)算方法
- 唐山高科總部大廈幕墻工程幕墻招標(biāo)技術(shù)評(píng)估總結(jié)
- 蘇教版三年級(jí)下冊(cè)數(shù)學(xué) 第三單元 解決問(wèn)題的策略 測(cè)試卷
- 10kV線路拆除
- 高中學(xué)生選課指導(dǎo)手冊(cè)
- 為老年人更換紙尿褲評(píng)分標(biāo)準(zhǔn)
- 教務(wù)管理系統(tǒng)UML模型PPT課件
- 吸收塔及煙囪施工方案
- 高中數(shù)學(xué)答題卡模板word版(共2頁(yè))
評(píng)論
0/150
提交評(píng)論