c 语言中的字符串操作

本章集中讨论字符串操作,包括拷贝字符串,拷贝字符串的一部分,比较字符串,字符串右对齐,删去字符串前后的空格,转换字符串,等等。C语言提供了许多用来处理字符串的标准库函数,本章将介绍其中的一部分函数。

在编写C程序时,经常要用到处理字符串的技巧,本章提供的例子将帮助你快速学会一些常用函数的使用方法,其中的许多例子还能有效地帮助你节省编写程序的时间。

61 串拷贝(strcpy)和内存拷贝(memcpy)有什么不同?它们适合于在哪种情况下使用?

strcpy()函数只能拷贝字符串。strcpy()函数将源字符串的每个字节拷贝到目录字符串中,当遇到字符串末尾的null字符(/0)时,它会删去该字符,并结束拷贝。

memcpy()函数可以拷贝任意类型的数据。因为并不是所有的数据都以null字符结束,所以你要为memcpy()函数指定要拷贝的字节数。

在拷贝字符串时,通常都使用strcpy()函数;在拷贝其它数据(例如结构)时,通常都使用memcpy()函数。

以下是一个使用strcpy()函数和memcpy()函数的例子:

#include < stdio.h >

#include
< string .h >

typedef
struct cust - str ... {

intid;

charlast_name[20];

charfirst_name[l5];

}
CUSTREC;

void main( void );

void main( void )

... {

char*src_string="Thisisthesourcestring";

chardest_string[50];

CUSTRECsrc_cust;

CUSTRECdest_cust;

printf(
"Hello!I'mgoingtocopysrc_stringintodest_string! ");

/*Copysrc_stringintodest-string.Noticethatthedestination

stringisthefirstargument.Noticealsothatthestrcpy()

functionreturnsapointertothedestination
string.*/

printf(
"Done!dest_stringis:%s ",

strcpy(dest_string,src_string));

printf(
"Encore!Let'scopyoneCUSTRECtoanother. ");

prinft(
"I'llcopysrc_custintodest_cust. ");

/*First,intializethesrc_custdatamembers.*/

src_cust.id
=1;

strcpy(src_cust.last_name,
"Strahan");

strcpy(src_cust.first_name,
"Troy");

/*Now,Usethememcpy()functiontocopythesrc-custstructureto

thedest_custstructure.Noticethat,just
aswithstrcpy(),the

destinationcomesfirst.
*/

memcpy(
&dest_cust,&src_cust,sizeof(CUSTREC));

printf(
"Done!Ijustcopiedcustomernumber#%d(%s%s).",

dest_cust.id,dest_cust.first_name,dest_cust.last_name);

}


6. 2怎样删去字符串尾部的空格?

C语言没有提供可删去字符串尾部空格的标准库函数,但是,编写这样的一个函数是很方便的。请看下例:

#include < stdio.h >

#include
< string .h >



void main( void );

char * rtrim( char * );

void main( void )

... {

char*trail_str="Thisstringhastrailingspacesinit";

/*Showthestatusofthestringbeforecallingthertrim()

function.
*/

printf(
"Beforecallingrtrim(),trail_stris'%s'i",trail_str);

print(
"andhasalengthof%d. ",strlen(trail_str));

/*CallthertrimOfunctiontoremovethetrailingblanks.*/

rtrim(trail_str);

/*Showthestatusofthestring

aftercallingthertrim()function.
*/

printf(
"Aftercallingrttim(),trail_stris'%s' ",trail_str);

printf(
"andhasalengthof%d. ",strlen(trail-str));

}


/ * Thertrim()functionremovestrailingspacesfroma string . * / .



char * rtrim( char * str)

... {

intn=strlen(str)-1;/*StartatthecharacterBEFORE

the
nullcharacter(0).*/

while(n>0)/*Makesurewedon'tgooutofhounds...*/

...{

if(*(str+n)1='')/*Ifwefindanonspacecharacter:*/

...{

*(str+n+1)='';/*Putthenullcharacteratone

characterpastourcurrent

position.
*/

break;/*Breakoutoftheloop.*/

}


else/*Otherwise,keepmovingbackwardinthestring.*/.

n
--;

}


returnstr;/**//*Returnapointertothestring*/

}


在上例中,rtrim()是用户编写的一个函数,它可以删去字符串尾部的空格。函数rtrim()从字符串中位于null字符前的那个字符开始往回检查每个字符,当遇到第一个不是空格的字符时,就将该字符后面的字符替换为null字符。因为在C语言中null字符是字符串的结束标志,所以函数rtrim()的作用实际上就是删去字符串尾部的所有空格。

6.3 怎样删去字符串头部的空格?

C语言没有提供可删去字符串头部空格的标准库函数,但是,编写这样的一个函数是很方便的。请看下例;

#include < stdio.h >

#include
< string .h >



void main( void );

char * ltrim( char * );

char * rtrim( char * );

void main( void )

... {

char*lead_str="Thisstringhasleadingspacesinit.";,

/*ShowthestatusofthestringbeforecallingtheItrim()

function.
*/

printf(
"BeforecallingItrim(),lead-stris'%s' ",lead_str);

printf(
"andhasalengthof%d. ",strlen(lead_str));

/*CalltheItrim()functiontoremovetheleadingblanks.*/.

Itrim(lead_str);

/*Showthestatusofthestring

aftercallingtheItrim()function.
*/

prinft(
"AftercallingItrim(),lead_stris'%s' ",lead_str);

print(
"andhasalengthof%d. '',strlen(lead-str));

}


/ * TheItrim()functionremovesleadingspacesfroma string . * /



char * ltrim( char * str)

... {

strrev(str);
/*CallstrrevOtoreversethestring.*/

rtrim(str)).
/**//*CallrtrimOtoremvoethe"trailing"spaces.
*/

strrev(str);/*Restorethestring'soriginalorder.*/

returnstr;/*Returnapointertothestring.*/.

}

/*Thertrim()functionremovestrailingspacesfromastring.*/



char*rtrim(char*str)

{

intn=strlen(str)-l;/*StartatthecharacterBEFORE

thenullcharacter(
<!--192.168.1.236-->
上一篇: 直线生成算法 下一篇: 移植ucgui到s3c2410十大步
<script type="text/javascript"> new Ad(4, 'ad_cen'); </script>
查看评论
* 以上用户言论只代表其个人观点,不代表优快云网站的观点或立场
<script type="text/javascript"> var fileName = '1389027'; var commentscount = 0; var islock = false </script><script type="text/javascript" src="http://static.blog.youkuaiyun.com/scripts/comment.js?v=1.8"></script>
<script type="text/javascript"> new Ad(5, 'ad_bot'); </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值