字符串处理函数的用法案例

/*
字符串处理函数:
>库函数:string.h
>函数原型:
char *strcpy( char *s1, const char *s2 );//copy字符串函数,返回s1的值,s2->s1;
char *strncpy( char *s1, const char *s2, size_t n );//s2->s1 至多复制n个字符
char *strcat( char *s1, const char *s2 );//connect字符串函数,返回s1的值,s1+s2;
char *strncat( char *s1, const char *s2 , size_t n );//s1 + s2中的至多n个字符.

//字符串比较函数:
int strcmp( const char *s1, const char *s2);
int strncmp( const char *s1, const char *s2, size_t n );

//字符串查找函数:
char *strchr( const char *s, int c );

char *strrchr( const char *s, int c );

char *strstr( char *s1, const char *s2 );
暂时不讲述:在"安全的C程序设计"中介绍

//字符串分解函数
char *strtok( char *s1, const char *s2 );


时间:Dec 29, 2017 13:28
*/

#include<stdio.h>
#include<string.h>
#define SIZE 100

int main()
{
    const char *s1 = "woyoukexiaolidezhi";
    const char *s2 = "is";
    const char *s3 = "man";
    char s4[SIZE];//创建字符串数组
    puts( strcpy( s4 , s1 ) );//进行字符串复制函数并且输出函数返回的字符指针
    puts( s4 );//输出s4,判断是否复制成功

    memset( s4, 0, sizeof(s4) );//将复制的函数清零
    strcat( s4, strcat( s4, s3));//进行字符串连接工作,在这里,s4原本为空,连接了"man"后与自己连接所以最后的s4为"manman";
    puts(s4);//输出s4;

    puts( strchr( s1, 'i') );//进行字符查找工作->首次找到返回指针
    puts( strrchr( s1, 'i') );//进行字符查找工作->最后一次找到返回指针
    puts( strstr( s1, "ao") );//进行字符串查找工作->首次找到返回指针

    return 0;
}

/****************
woyoukexiaolidezhi
woyoukexiaolidezhi
manman
iaolidezhi
i
aolidezhi
请按任意键继续. . .

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值