这使得head指针指向heart数组的首元素。
5.字符串数组
sizeofmytalents:40,sizeofyourtalents:200
11.1.2指针和字符串
11.2字符串输入
11.2.1分配空间
char*name;
charname[81];
11.2.3gets()的替代品
程序清单11.7演示了fgets()和fputs()函数的用法。
Enterastring,please.
applepie
Yourstringtwice(puts(),thenfputs()):
Enteranotherstring,please.
strawberryshortcake
strawberrysh
strawberryshDone.
continue;//常规处理多余字符方法
空字符和空指针
2.gets_s()函数(略)
3.s_gets()函数
11.2.4scanf()函数
开始--第一个非空白字符。
11.3字符串输出
11.3.1puts()函数
Iama#definedstring.
Anarraywasinitializedtome.
Apointerwasinitializedtome.
11.3.2fputs()函数
fputs()函数是puts()针对文件定制的版本。它们的区别如下。
charline[81];
while(fgets(line,81,stdin))
fputs(line,stdout);
fputs(line,stdout);//fputs()函数格式
11.3.3printf()函数
puts(string);
11.4自定义输入/输出函数
while(*string);
注意
Ineverwouldcryoldchairstomend.
Icount37characters.
11.5字符串函数
11.5.1strlen()函数
{
if(strlen(string)>size)
}
程序清单11.17中的程序测试了fit()函数。注意代码中使用了C字符串常量的串联特性。
Thingsshouldbeassimpleaspossible,butnotsimpler.
Thingsshouldbeassimpleaspossible
butnotsimpler.
11.5.2strcat()函数
Whatisyourfavoriteflower
wonderflower
wonderflowerssmelllikeoldshoes.
ssmelllikeoldshoes.
bye
11.5.3strncat()函数
Rose
Rosessmelllikeoldshoes.
Whatisyourfavoritebug
Aphid
Aphidssmell
11.5.4strcmp()函数
修改后的版本如程序清单11.21所示。
1.strcmp()的返回值
程序清单11.23用strcmp()函数检查程序是否要停止读取输入。
2.strncmp()函数
Found:astronomy
Found:astrophysics
Found:astrophobia
Thelistcontained3wordsbeginningwithastro.
11.5.5strcpy()和strncpy()函数
Enter5wordsbeginningwithq:
quackery
quasar
quilt
quotient
nomore
quiz
Herearethewordsaccepted:
chartarget[20];
intx;
char*str;
1.strcpy()的其他属性
beast
Bethebestthatyoucanbe.
Bethebeast
quack
quadratic
quisling
quota
quagga
quadra
quisli
strncpy(qwords[i],temp,TARGSIZE-1);
11.5.6sprintf()函数
Enteryourfirstname:
Annie
Enteryourlastname:
vonWurstkasse
Enteryourprizemoney:
25000
vonWurstkasse,Annie:$25000.00
11.5.7其他字符串函数
char*strcpy(char*restricts1,constchar*restricts2);
char*strncpy(char*restricts1,constchar*restricts2,size_tn);
char*strcat(char*restricts1,constchar*restricts2);
该函数把s2指向的字符串拷贝至s1指向的字符串末尾。s2字符串的第1个字符将覆盖s1字符串末尾的空字符。该函数返回s1。
char*strncat(char*restricts1,constchar*restricts2,size_tn);
intstrcmp(constchar*s1,constchar*s2);
char*strchr(constchar*s,intc);//字符串中查找字符函数
char*strpbrk(constchar*s1,constchar*s2);
char*strrchr(constchar*s,intc);
char*strstr(constchar*s1,constchar*s2);
size_tstrlen(constchar*s);
charline[80];
char*find;
fgets(line,80,stdin);
Inputupto20lines,andIwillsortthem.
OthatIwaswhereIwouldbe,
ThenwouldIbewhereIamnot;
ButwhereIamImustbe,
AndwhereIwouldbeIcannot.
11.6.1排序指针而非字符串
11.6.2选择排序算法
现在来进一步分析选择排序的过程。
forn-第2个元素至最后一个元素,
11.7ctype.h字符函数和字符串
Pleaseenteraline:
Thatlinehas4punctuationcharacters.
if(islower(*str))/*ANSIC之前的做法--在转换大小写之前先检查*/
$fuss
C>fuss
$fuss-rGinger
11.9把字符串转换为数字
程序清单11.32运行结果
longstrtol(constchar*restrictnptr,char**restrictendptr,intbase);
Enteranumber(emptylinetoquit):
10
base10input,base10output:10,stoppedat(0)
base16input,base10output:16,stoppedat(0)
Nextnumber:
10atom
base10input,base10output:10,stoppedatatom(97)
base16input,base10output:266,stoppedattom(116)
Enter//直接敲换行符
Bye!
11.10关键概念
11.11本章小结
atoi()、atol()和atof()函数把字符串形式的数字分别转换成int、long和double类型的数字。strtol()、strtoul()和strtod()函数把字符串形式的数字分别转换成long、unsignedlong和double类型的数字。