Character字元字符单字_第1页
Character字元字符单字_第2页
Character字元字符单字_第3页
Character字元字符单字_第4页
Character字元字符单字_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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變量/變數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指標 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' 結束字符.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指標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)是否數字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)是否數字或字母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)轉小楷字母If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument toupper(int c)轉大楷字母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)文字轉小數Converts the string nPtr to atoi(const char *nPtr)文字轉整數Converts the str

10、ing nPtr to int.long atol(const char *nPtr)文字轉長整數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字長/字數The length of a string does not inclu

12、de the '0' character.Function prototypeFunction descriptionsize_t strlen(const char *s)字長/字數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小心字數/長度.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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论