没有合适的资源?快使用搜索试试~ 我知道了~
C语言字符串操作函数.doc
0 下载量 129 浏览量
2022-12-02
19:08:26
上传
评论
收藏 107KB DOC 举报
温馨提示
试读
22页
C语言字符串操作函数.doc
资源推荐
资源详情
资源评论
C 语言字符串操作函数.txt37 真诚是美酒,年份越久越醇香浓烈;真诚是焰火,在高处绽放
才愈显美丽;真诚是鲜花,送之于人,手有余香。C 语言字符串操作
字符串操作--c 语言
--------------------------------------------------------------------------------
本章集中讨论字符串操作,包括拷贝字符串,拷贝字符串的一部分,比较字符串,字符串右
对齐,删去字符串前后的空格,转换字符串,等等。C 语言提供了许多用来处理字符串的标
准库函数,本章将介绍其中的一部分函数。
在编写 C 程序时,经常要用到处理字符串的技巧,本章提供的例子将帮助你快速学会一
些常用函数的使用方法,其中的许多例子还能有效地帮助你节省编写程序的时间。
6.1 串拷贝(strcpy)和内存拷贝(memcpy)有什么不同?它们适合于在哪种情况下使用?
strcpy()函数只能拷贝字符串。strcpy()函数将源字符串的每个字节拷贝到目录字符串
中,当遇到字符串末尾的 null 字符( )时,它会删去该字符,并结束拷贝。
memcpy()函数可以拷贝任意类型的数据。因为并不是所有的数据都以 null 字符结束,所
以你要为 memcpy()函数指定要拷贝的字节数。
在拷贝字符串时,通常都使用 strcpy()函数;在拷贝其它数据(例如结构)时,通常都
使用 memcpy()函数。
以下是一个使用 strcpy()函数和 memcpy()函数的例子:
#include <stdio. h>
#include <string. h>
typedef struct cust-str {
int id ;
char last_name [20] ;
char first_name[l5];
} CUSTREC;
void main (void);
void main (void)
{
char * src_string = "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!");
/ * Copy src_ string into dest-string. Notice that the destination
string is the first argument. Notice also that the strcpy()
function returns a pointer to the destination string. * /
printf("Done! dest_string is: %s" ,
strcpy(dest_string, src_string)) ;
printf("Encore! Let's copy one CUSTREC to another. ") ;
prinft("I'll copy src_cust into dest_cust. ");
/ * 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 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 怎样拷贝字符串的一部分?
6.7 怎样打印字符串的一部分?
6. 2 怎样删去字符串尾部的空格?。
C 语言没有提供可删去字符串尾部空格的标准库函数,但是,编写这样的一个函数是很
方便的。请看下例:
#include <stdio. h>
# include <string. h>
void main (void);
char * rtrim(char * );
void main(void)
{
char * trail_str = "This string has trailing spaces in it";
/ * Show the status of 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) ;
/ * 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 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 + 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. * /
}
else / * Otherwise , keep moving backward in the string. * /.
n--;
}
return str; /*Return a pointer to the string*/
}
在上例中,rtrim()是用户编写的一个函数,它可以删去字符串尾部的空格。函数 rtrim()
从字符串中位于 null 字符前的那个字符开始往回检查每个字符,当遇到第一个不是空格的字
符时,就将该字符后面的字符替换为 null 字符。因为在 C 语言中 null 字符是字符串的结束
标志,所以函数 rtrim()的作用实际上就是删去字符串尾部的所有空格。
请参见:
6.3 怎样删去字符串头部的空格?
6.5 怎样将字符串打印成指定长度?
6.3 怎样删去字符串头部的空格?
C 语言没有提供可删去字符串头部空格的标准库函数,但是,编写这样的一个函数是很
方便的。请看下例;
#include <stdio. h>
#include <string. h>
void main(void);
char * ltrim (char * ) ;
char * 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 length 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);
print("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. * /
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 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
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. */
}
在上例中,删去字符串头部空格的工作是由用户编写的 ltrim()函数完成的,该函数调
用了·6.2 的例子中的 rtrim()函数和标准 C 库函数 strrev()。ltrim()函数首先调用
strrev()函数将字符串颠倒一次,然后调用 rtrim()函数删去字符串尾部的空格,最后调用
strrev()函数将字符串再颠倒一次,其结果实际上就是删去原字符串头部的空格。
请参见:
6.2 怎样删去字符串尾部的空格?
6.5 怎样将字符串打印成指定长度?
6.4 怎样使字符串右对齐?
C 语言没有提供可使字符串右对齐的标准库函数,但是,编写这样的一个函数是很方便
的。请看下例:
#include <stdio. h>
#include <string. h>
#include <malloc. h>
void main (void);
char * r just (char * ) ;
剩余21页未读,继续阅读
资源评论
黑色的迷迭香
- 粉丝: 705
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
- Apache Flink 未授权访问+远程代码执行.pdf
- hadoop-基于hive的聊天数据分析报表可视化案例数据源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功