ImageVerifierCode 换一换
格式:DOC , 页数:22 ,大小:107KB ,
资源ID:4484299      下载积分:5 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/4484299.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(C语言字符串操作函数.doc)为本站上传会员【二***】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

C语言字符串操作函数.doc

1、C语言字符串操作函数.txt37真诚是美酒,年份越久越醇香浓烈;真诚是焰火,在高处绽放才愈显美丽;真诚是鲜花,送之于人,手有余香。C语言字符串操作 字符串操作--c语言 -------------------------------------------------------------------------------- 本章集中讨论字符串操作,包括拷贝字符串,拷贝字符串的一部分,比较字符串,字符串右对齐,删去字符串前后的空格,转换字符串,等等。C语言提供了许多用来处理字符串的标准库函数,本章将介绍其中的一部分函数。 在编写C程序时,经常要用到处理字符串的技巧

2、本章提供的例子将帮助你快速学会一些常用函数的使用方法,其中的许多例子还能有效地帮助你节省编写程序的时间。 6.1 串拷贝(strcpy)和内存拷贝(memcpy)有什么不同?它们适合于在哪种情况下使用? strcpy()函数只能拷贝字符串。strcpy()函数将源字符串的每个字节拷贝到目录字符串中,当遇到字符串末尾的null字符(\0)时,它会删去该字符,并结束拷贝。 memcpy()函数可以拷贝任意类型的数据。因为并不是所有的数据都以null字符结束,所以你要为memcpy()函数指定要拷贝的字节数。 在拷贝字符串时,通常都使用strcp

3、y()函数;在拷贝其它数据(例如结构)时,通常都使用memcpy()函数。 以下是一个使用strcpy()函数和memcpy()函数的例子: #include #include typedef struct cust-str { int id ; char last_name [20] ; char first_name[l5]; } CUSTREC; void main (void); void main (void) { char * src_string

4、 "This is the source string" ; char dest_string[50]; CUSTREC src_cust; CUSTREC dest_cust; printf("Hello! I'm going to copy src_string into dest_string!\n"); / * Copy src_ string into dest-string. Notice that the destination string is the first argument. Notice

5、also that the strcpy() function returns a pointer to the destination string. * / printf("Done! dest_string is: %s\n" , strcpy(dest_string, src_string)) ; printf("Encore! Let's copy one CUSTREC to another. \n") ; prinft("I'll copy src_cust into dest_cust. \n");

6、 / * First, intialize the src_cust data members. * / src_cust. id = 1 ; strcpy(src_cust. last_name, "Strahan"); strcpy(src_cust. first_name, "Troy"); / * Now, Use the memcpy() function to copy the src-cust structure to the dest_cust structure. Notice that, just as

7、 with strcpy(), the destination comes first. * / memcpy(&dest_cust, &src_cust, sizeof(CUSTREC)); printf("Done! I just copied customer number # %d (%s %s). " , dest_cust. id, dest_cust. first_name, dest_cust. last_name) ; } 请参见: 6.6怎样拷贝字符串的一部分?

8、6.7怎样打印字符串的一部分? 6. 2怎样删去字符串尾部的空格?。 C语言没有提供可删去字符串尾部空格的标准库函数,但是,编写这样的一个函数是很方便的。请看下例: #include # include void main (void); char * rtrim(char * ); void main(void) { char * trail_str = "This string has trailing spaces in it"; / * Show the status o

9、f the string before calling the rtrim() function. * / printf("Before calling rtrim(), trail_str is '%s'\fi" , trail_str); print ("and has a length of %d. \n" , strlen (trail_str)); / * Call the rtrimO function to remove the trailing blanks. * / rtrim(trail_str) ;

10、 / * Show the status of the string after calling the rtrim() function. * / 2006-6-7 12:41 回复 210.77.88.* 2楼 printf("After calling rttim(), trail_ str is '%s'\n", trail _ str ); printf ("and has a length of %d. \n" , strlen(trail-str)) ; } / * The rtrim() function

11、removes trailing spaces from a string. * /. char * rtrim(char * str) { int n = strlen(str)-1; / * Start at the character BEFORE the null character (\0). * / while (n>0) / * Make sure we don't go out of hounds. . . * / { if ( * (str +

12、n) 1 =' ') / * If we find a nonspace character: * / { * (str+n+1) = '\0' ; / * Put the null character at one character past our current position. * / break ; / * Break out of the loop.

13、 * / } else / * Otherwise , keep moving backward in the string. * /. n--; } return str; /*Return a pointer to the string*/ } 在上例中,rtrim()是用户编写的一个函数,它可以删去字符串尾部的空格。函数rtrim()从字符串中位于null字符前的那个字符开始往回检查每个字符,当遇到第一个不是空格的字符时,就将该字符后面的字符替换为null字符。因为在C语言中

14、null字符是字符串的结束标志,所以函数rtrim()的作用实际上就是删去字符串尾部的所有空格。 请参见: 6.3怎样删去字符串头部的空格? 6.5怎样将字符串打印成指定长度? 6.3 怎样删去字符串头部的空格? C语言没有提供可删去字符串头部空格的标准库函数,但是,编写这样的一个函数是很方便的。请看下例; #include #include void main(void); char * ltrim (char * ) ; char *

15、 rtrim(char * ) ; void main (void) { char * lead_str = " This string has leading spaces in it. " ;, / * Show the status of the string before calling the Itrim() function. * / printf("Before calling Itrim(), lead-str is '%s'\n", lead_str); printf("and has a lengt

16、h of %d. \n" , strlen(lead_str)); / * Call the Itrim() function to remove the leading blanks. * /. Itrim(lead_str); / * Show the status of the string after calling the Itrim() function. * / prinft("After calling Itrim(), lead_str is '%s'\n", lead_str); pri

17、nt("and has a length of %d. \n'' , strlen(lead-str)) ; } / * The Itrim() function removes leading spaces from a string. * / char * ltrim(char * str) { strrev(str) ; / * Call strrevO to reverse the string. * / rtrim(str)). /* Call rtrimO to remvoe the "trailing" spaces. * /

18、strrev(str); / * Restore the string's original order. * / return str ; / * Return a pointer to the string. * /. } / * The rtrim() function removes trailing spaces from a string. * / char* rtrim(char* str) { int n = strlen (str)-l ; / * Start at the character BEFORE the null

19、 character (\0). * / while (n>0) / * Make sure we don't go out of bounds... * /. { if ( * (str+n) ! =' ') If we find a nonspace character: * / { * (str+n + 1) = '\0' ; / * Put the null character at one character past our current

20、 position. * / break;j / * Break out of the loop. * / 2006-6-7 12:41 回复 210.77.88.* 3楼 } else / * Otherwise, keep moving backward in the string. * / n --; } return str; /* Return a pointer tO the string. */ } 在上例中

21、删去字符串头部空格的工作是由用户编写的ltrim()函数完成的,该函数调用了·6.2的例子中的rtrim()函数和标准C库函数strrev()。ltrim()函数首先调用strrev()函数将字符串颠倒一次,然后调用rtrim()函数删去字符串尾部的空格,最后调用strrev()函数将字符串再颠倒一次,其结果实际上就是删去原字符串头部的空格。 请参见: 6.2怎样删去字符串尾部的空格? 6.5怎样将字符串打印成指定长度? 6.4 怎样使字符串右对齐? C语言没有提供可使字符串右对齐的标准库函数,但是,编写这样的

22、一个函数是很方便的。请看下例: #include #include #include void main (void); char * r just (char * ) ; char * rtrim(char * ); void main (void) { char * rjust_str = "This string is not righ-justified. " ; / * Show the status of the string before calling

23、the rjust() function. * / printf("Before calling rjust(), rjust_str is ' %s'\n. " , rjust_str); / * Call the rjustO function to right-justify this string. * / rjust(rjust_str) ; / * Show the status of the string after calling the rjust() function. * /

24、 printf ("After calling rjust() , rjust_str is ' %s'\n. " , rjust_str) ; } / * The rjust() function right-justifies a string. * / char * r just (char * str) { int n = strlen(str); / * Save the original length of the string. * / char* dup_str; dup_str = strdup(str); / * Mak

25、e an exact duplicate of the string. * / rtrim(dup_str); /* Trim off the trailing spaces. */ / * Call sprintf () to do a virtual "printf" back into the original string. By passing sprintf () the length of the original string, we force the output to be the same size as

26、the original, and by default the sprintf() right-justifies the output. The sprintf() function fills the beginning of the string with spaces to make it the same size as the original string. * / sprintf(str, "%*. * s", n, n, dup_str); free(dup-str) ; / * Free

27、the memory taken by the duplicated string. * / return str;\ / * Return a pointer to the string. * / } / * The rtrim() function removes trailing spaces from a string. * / char * rtrim(char * str) { int n = strlen(str)-l; / * Start at the character BEFORE the null

28、 character (\0). * / while (n>0) / * Make sure we don't go out of bounds... * / { if ( * (str+n) ! = ' ') / * If we find a nonspace character: * / { * (str + n + 1) = '\0';( / * Put the null character at one

29、 character past our current position. * / break; / * Break out of the loop. * / } else / * Otherwise, keep moving backward in the string. * / n—; } return str ; / * Return a point

30、er to the string. * / } 在上例中,使字符串右对齐的工作是由用户编写的rjust()函数完成的,该函数调用了6.2的例子中的rtrim()函数和几个标准函数。rjust()函数的工作过程如下所示: 2006-6-7 12:41 回复 210.77.88.* 4楼 (1) 将原字符串的长度存到变量n中。这一步是不可缺少的,因为输出字符串和原字符串的长度必须相同。 (2) 调用标准C库函数strdup(),将原字符串复制到dup_str中。原字符串需要有一份拷贝,因为经过右对齐处理的字符串要写到原字符串中。

31、 (3) 调用rtrim()函数,删去dup_str尾部的空格。 (4) 调用标准C库函数sprinf(),将dup_str写到原字符串中。由于原字符串的长度(存在n中)被传递给sprintf()函数,所以迫使输出字符串的长度和原字符串相同。因为sprintf()函数缺省使输出字符串右对齐,因此输出字符串的头部将被加入空格,以使它和原字符串长度相同,其效果实际上就是使原字符串右对齐。 (5)调用标准库函数free(),释放由strdup()函数分配给dup_str的动态内存。 请参见: 6.5怎样将字符串打印成指定长度?

32、 6.5 怎样将字符串打印成指定长度? 如果要按表格形式打印一组字符串,你就需要将字符串打印成指定长度。利用printf()函数可以很方便地实现这一点,请看下例: # include char * data[25] = { "REGION", "--Q1--", "--Q2--", "--Q3--", "--Q4--", "North" , "10090. 50" , "12200. 10" , "26653.12" , "62634. 32" , "South", "21662.37", "

33、95843.23", "23788.23", "48279.28", "East", "23889.38", "23789.05", "89432.84", "29874.48", "West", "85933.82", "74373.23", "78457.23", "28799.84" }; void main (void) ; void main (void) { int x; fox (x = 0, x<25; x+ + ) { if ((x % 5) == 0&&(x !=0))

34、 printf("\n"); printf (" %-10. 10s" , data[x]) ; } } 在上例中,字符串数组char *data[]中包含了某年4个地区的销售数据。显然,你会要求按表格形式打印这些数据,而不是一个挨一个地毫无格式地打印这些数据。因此,上例中用下述语句来打印这些数据: printf("%-10.10s",data[x]); 参数"%-10.10s"指示printf()函数按10个字符的长度打印一个字符串。在缺省情况下,printf()函数按右对齐格式打印字符串,但是,在第一个

35、10的前面加上减号(-)后,prinft()函数,就会使字符串左对齐。为此,printf()函数会在字符串的尾部加入空格,以使其长度达到10个字符。上例的打印输出非常整洁,类似于一张表格,如下所示: REGION --Q1-- --Q2-- --Q3-- --Q4-- North 10090.50 12200.10 26653.12 62634.32 SOuth 21662.37 95843.23 23788.23 48279.28 East 23889.38 23789

36、05 89432.84 29874.48 West 85933.82 74373.23 78457.23 28799.84 请参见: 6.4怎样使字符串右对齐? 6.6.怎样拷贝字符串的一部分? 利用标准库函数strncpy(),可以将一字符串的一部分拷贝到另一个字符串中。strncpy()函数有3个参数:第一个参数是目录字符串;第二个参数是源字符串;第三个参数是一个整数,代表要从源字符串拷贝到目标字符串中的字符数。以下是一个用strncpy()函数拷贝字符串的一部分的例子:

37、 # include # include void main(void); void main (void) { char * source_str = "THIS IS THE SOURCE STRING" ; char dest_strl[40]= {0}, dest_str2[40]= {0}; / * Use strncpy() to copy only the first 11 characters. * / strncpy(dest_strl, source-str,

38、11); printf("How about that! dest-strl is now: '%s'!!!\n", dest-strl); / * Now, use strncpy() to copy only the last 13 characters. * / strncpy(dest_strl, source_str + (strlen(source_str)-l3) , 13); 2006-6-7 12:41 回复 210.77.88.* 5楼 printf("Whoa! dest_str2 is now: '%

39、s'!!!\n". dest_str2); } 在上例中,第一次调用strncpy()函数时,它将源字符串的头11个字符拷贝到dest_str1中,这是一种相当直接的方法,你可能会经常用到。第二次调用strncpy()函数时,它将源字符串的最后13个字符拷贝到dest_str2中,其实现过程为: (1)用strlen()函数计算出source_str字符串的长度,即strlen(source_str)。 (2)将source_str的长度减去13(13是将要拷贝的字符数),得出source_str中剩余的字符数,即pstrlen(source_str)-

40、13。 (3)将strlen(source_str)-13和source_str的地址相加,得出指向source_str中倒数第13个字符的地址的指针,即source_str+(strlen(source_str)-13)。这个指针就是strncpy()函数的第二个参数。 (4)在strncpy()函数的第三个参数中指定要拷贝的字符是13。 上例的打印输出如下所示: How about that! dest_str1 is now:'THIS IS THE'!!! Whoa! dest_str2 is now:'SOURCE STRING'!

41、 需要注意的是,在将source_str拷贝到dest_strl和dest_str2之前,dest_strl和dest_str2都要被初始化为null字符(\0)。这是因为strncpy()函数在拷贝字符串时不会自动将null字符添加到目录字符串后面,因此你必须确保在目标字符串的后面加上null字符,否则会导致打印出一些杂乱无章的字符。 请参见; 6.1 串拷贝(strcpy)和内存拷贝(memcpy)有什么不同?它们适合于在哪种情况下使用? 6.9 怎样打印字符串的一部分? 6.7 怎样将数字转换为字符串? C

42、语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转换为字符串的一个例子: # include # include void main (void); void main (void) { int num = 100; char str[25]; itoa(num, str, 10); printf("The number 'num' is %d and the string 'str' is %s. \n" ,

43、 num, str); } itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。在上例中,转换基数为10。 下列函数可以将整数转换为字符串: ---------------------------------------------------------- 函数名 作 用 -------------------------------------------------------

44、 itoa() 将整型值转换为字符串 itoa() 将长整型值转换为字符串 ultoa() 将无符号长整型值转换为字符串 ---------------------------------------------------------- 请注意,上述函数与ANSI标准是不兼容的。能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数,请看下例: #include # include

45、dlib. h> void main (void); void main (void) { int num = 100; char str[25]; sprintf(str, " %d" , num); printf ("The number 'num' is %d and the string 'str' is %s. \n" , num, str); } 在将浮点型数字转换为字符串时,需要使用另外一组函数。以下是用fcvt()函数将浮点型值转换为字符串

46、的一个例子: # include # include void main (void); void main (void) { double num = 12345.678; char * sir; int dec_pl, sign, ndigits = 3; /* Keep 3 digits of precision. * / str = fcvt(num, ndigits, &dec-pl, &sign); /* Convert the float

47、 to a string. * / 2006-6-7 12:41 回复 210.77.88.* 6楼 printf("Original number; %f\n" , num) ; /* Print the original floating-point value. * /

48、 printf ("Converted string; %s\n",str); /* Print the converted string's value. * / printf ("Decimal place: %d\n" , dec-pi) ; /* Print the location of the decimal point. * / printf

49、 ("Sign: %d\n" , sign) ; /* Print the sign. 0 = positive, 1 = negative. * / } fcvt()函数和itoa()函数有数大的差别。fcvt()函数有4个参数:第一个参数是要转换的浮点型值;第二个参数是转换结果中十进制小数点右侧的位数;第三个参数是指向一个整数的指针,该整数用

50、来返回转换结果中十进制小数点的位置;第四个参数也是指向一个整数的指针,该整数用来返回转换结果的符号(0对应于正值,1对应于负值)。 需要注意的是,fcvt()函数的转换结果中并不真正包含十进制小数点,为此,fcvt()函数返回在转换结果中十进制小数点应该占据的位置。在上例中,整型变量dec_pl的结果值为5,因为在转换结果中十进制小数点应该位于第5位后面。如果你要求转换结果中包含十进制小数点,你可以使用gcvt()函数(见下表)。 下列函数可以将浮点型值转换为字符串: -------------------------------------------------

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服