13 字符串的复制、连接、比较、查找

2、字符串的复制

#include <stdio.h>

#include <string.h>

int main(int argc, char **argv)

{

char dest[128] = {};

char src[128] = "hello";

/*dest = src;*/

strcpy(dest, src);

printf("dest : %s\n", dest);

/*dest = "hoa are you";*/

strcpy(dest, "how are you");

printf("dest : %s\n", dest);

//dest = "how a re you"

//src =  "hel lo"  how alo

strcpy(dest + 20, src + 3);

printf("dest : %s\n", dest);

printf("dest : %s\n", dest + 20);

bzero(dest, sizeof(dest));

printf("dest : %s\n", dest);

strncpy(dest, src + 2, 2);

printf("dest : %s\n", dest);

return 0;

}

====================================================

4、字符串的连接strcat

#include <stdio.h>

#include <string.h>

int main(int argc, char **argv)

{

char s1[128] = "hello";

char s2[128] = "world";

/*strcat(s1, s2);*/

/*strcat(s2, s1);*/

strncat(s2, s1, 2);

printf("s1 : %s\n", s1);

printf("s2 : %s\n", s2);

return 0;

}

==============================================================

5、字符串的比较

#include <stdio.h>

#include <string.h>

int main(int argc, char **argv)

{

char s1[128] = "make";

char s2[128] = "make";

printf("strcmp(make, mask) = %d\n", strcmp("make", "mask"));

printf("strcmp(make, make) = %d\n", strcmp("make", "make"));

printf("strcmp(mask, make) = %d\n", strcmp("mask", "make"));

printf("strcmp(makee, make) = %d\n", strcmp("makee", "make"));

printf("strncmp(make, mask, 2) = %d\n", strncmp("make", "mask", 2));

/*

 *    int a = 10, b = 10;

 *

 *    if (s1 == s2)

 *    {

 *        printf("there two number are some!\n");

 *    }

 *    else

 *    {

 *        printf("there two number are diff!\n");

 *    }

 */

return 0;

}

====================================================

7、字符串的查找

#include <stdio.h>

#include <string.h>

int main(int argc, char **argv)

{

printf("strchr(hello, l) = %s\n", strchr("hello", 'l'));

printf("strrchr(hello, l) = %s\n", strrchr("hello", 'l'));

printf("strchr(hello, l) = %s\n", strchr("hello", 'L'));

printf("==================\n");

printf("strstr(hello, lo) = %s\n", strstr("hello", "lo"));

printf("strstr(hello, lo) = %s\n", strstr("hello", "Lo"));

printf("strstr(hello, lo) = %s\n", strstr("hello", "l"));

printf("strcasestr(hello, Lo) = %s\n", strcasestr("hello", "Lo"));

/*printf("strstr(hello, lo) = %s\n", strstr("hello", 'l'));*/

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值