【string总结之三】C语言strcat/strncat

C语言string的几个函数虽然比较简单, 但是还是想总结在这里, 以免每次用到都要去查一下

strtol,strstr,strcat/strncat,strcpy/strncpy,strcmp/strncmp

1.strcat

【头文件与函数原型】

#include <string.h>

char *strcat(char *dest, const char *src);

【函数功能】

he  strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminat‐ing null byte.  The strings may not overlap, and the dest string must have enough space for the result.

【注意】

其实strcat没有什么好说的,但是有一点,src字符串到底找到哪里然后接到dest字符串上面?这里有可能会产生越界问题

如果我的dest是char dest[] = {'a', 'b', '\0', 'c', 'd', 'e', 'f', 'g'};而char src[] = {'W', 'X', 'Y', 'Z'};这种情况strcat(dest, src)就有可能出现越界问题,src会一直去找,直到找到'Z'后的0为止,并且还要将0拷贝到dest,运气好的话'Z'后面恰好是0,但是也属于非法内存访问!

网上有一种简单的实现如下:其核心就是先找到dest的'\0'处,再将src从开始到第一个'\0'(包括'\0')memcpy过去

char * strcat(char * dest, const char * src)
{
        char *tmp = dest;
 
        while (*dest)
                dest++;
        while ((*dest++ = *src++) != '\0')
                ;
 
        return tmp;
}

2.strncat

【头文件与函数原型】

#include <string.h>

char *strncat(char *dest, const char *src, size_t n);

【函数功能】

The strncat() function is similar, except that

*  it will use at most n characters from src; and

*  src does not need to be null-terminated if it contains n or more characters.

As with strcat(), the resulting string in dest is always null-terminated.

If src contains n or more characters, strncat() writes n+1 characters to dest (n from src plus the terminating null byte).  Therefore, the size of dest must be at least strlen(dest)+n+1.

MAN手册给出了简单的strncat的实现,我们可以用它来验证strncat的工作机制

char* strncat(char *dest, const char *src, size_t n) {
           size_t dest_len = strlen(dest);
           size_t i;

           for (i = 0 ; i < n && src[i] != '\0' ; i++)
                  dest[dest_len + i] = src[i];
           dest[dest_len + i] = '\0';

           return dest;
 }

root@ubuntu:/lianxi/lianxi_oj/string# gcc strcat.c -o c.out
root@ubuntu:/lianxi/lianxi_oj/string# ./c.out
s1_sa[0] = 97 s1_sa[1] = 98 s1_sa[2] = 87 s1_sa[3] = 88 s1_sa[4] = 0 s1_sa[5] = 101 s1_sa[6] = 102 s1_sa[7] = 103 
s1_my[0] = 97 s1_my[1] = 98 s1_my[2] = 87 s1_my[3] = 88 s1_my[4] = 0 s1_my[5] = 101 s1_my[6] = 102 s1_my[7] = 103 
s1_my_n[0] = 97 s1_my_n[1] = 98 s1_my_n[2] = 87 s1_my_n[3] = 88 s1_my_n[4] = 0 s1_my_n[5] = 101 s1_my_n[6] = 102 s1_my_n[7] = 103 
root@ubuntu:/lianxi/lianxi_oj/string# 
  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 void myStrncat(char* dest, char* src, int n)
  5 {
  6     int dest_len = strlen(dest);
  7     int i = 0;
  8     for(i = 0; i < n && src[i] != '\0'; i++)
  9     {
 10         dest[dest_len+i] = src[i];
 11     }
 12     dest[dest_len+i] = '\0';
 13     return;
 14 }
 15 
 16 int main(int argc, char* argv[])
 17 {
 18     char s1_sa[8] = {'a', 'b', '\0', 'c', 'd', 'e', 'f', 'g'};//use strcat
 19     char s1_my[8] = {'a', 'b', '\0', 'c', 'd', 'e', 'f', 'g'};//use myStrncat strlen(dest)
 20     char s1_my_n[8] = {'a', 'b', '\0', 'c', 'd', 'e', 'f', 'g'};//use myStrncat random length
 21     char s2[4] = {'W', 'X', '\0', 'Z'};
 22 
 23     strcat(s1_sa, s2);
 24     int i = 0;
 25     for(i = 0; i < sizeof(s1_sa)/sizeof(s1_sa[0]); i++)
 26     {
 27         printf("s1_sa[%d] = %d ", i, s1_sa[i]);
 28     }
 29     putchar(10);
 30 
 31     myStrncat(s1_my, s2, strlen(s2));
 32     for(i = 0; i < sizeof(s1_my)/sizeof(s1_my[0]); i++)
 33     {
 34         printf("s1_my[%d] = %d ", i, s1_my[i]);
 35     }
 36     putchar(10);
 37 
 38     myStrncat(s1_my_n, s2, sizeof(s2));
 39     for(i = 0; i < sizeof(s1_my_n)/sizeof(s1_my_n[0]); i++)
 40     {
 41         printf("s1_my_n[%d] = %d ", i, s1_my_n[i]);
 42     }
 43     putchar(10);
 44     return 0;
 45 }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值