【string总结之二】C语言strcpy/strncpy

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

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

1.strcpy

【头文件与函数原型】

#include <string.h>

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

【函数功能】(注意点就是strcpy连同字符串结束标志'\0'一同拷贝)

The  strcpy()  function  copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

2.strncpy

【头文件与函数原型】

#include <string.h>

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

【函数功能】

The strncpy() function is similar, except that at most n bytes of src are copied.  Warning: If there is no null byte among the first n  bytes  of  src,  the string placed in dest will not be null-terminated. If the length of src is less than n, strncpy() pads the remainder of dest with null bytes.

这里需要注意几点:第一是最多从源串中拷贝n个字节到目标串,第二是如果源串的前n个字节中没有'\0',那么子串有可能不是以'\0'结束,第三点是如果源串不够n个字节,那么会拷贝完源串(strlen)后,在字串中添加'\0'来凑够n个字节

MAN手册给出了一种简易的strncpy实现,我们可以通过它来验证记住strncpy的工作机制

A simple implementation of strncpy() might be:

char* strncpy(char *dest, const char *src, size_t n) {
           size_t i;

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

           return dest;
  }

【栗子】

root@ubuntu:/lianxi/lianxi_oj/string# gcc strncpy.c -o b.out
root@ubuntu:/lianxi/lianxi_oj/string# 
root@ubuntu:/lianxi/lianxi_oj/string# ls
a.out  b.out  strncpy.c  strtol.c
root@ubuntu:/lianxi/lianxi_oj/string# ./b.out
=====TEST ONE=====
dest1[0] = 97 dest1[1] = 98 dest1[2] = 99 dest1[3] = 100 dest1[4] = 101 

=====TEST TWO=====
dest2[0] = 97 dest2[1] = 98 dest2[2] = 99 dest2[3] = 100 dest2[4] = 0 dest2[5] = 0 dest2[6] = 0 dest2[7] = 0 dest2[8] = 0 dest2[9] = 0 
root@ubuntu:/lianxi/lianxi_oj/string# 
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 void myStrncpy(char* dest, char* src, int n)
  4 {   
  5     int i = 0;
  6     for(i = 0; i < n && src[i] != '\0'; i++)
  7     {   
  8         dest[i] = src[i];
  9     }
 10     for(;i < n; i++)
 11     {   
 12         dest[i] = '\0';
 13     }
 14     return;
 15 }
 16 
 17 int main(int argc, char* argv[])
 18 {   
 19     printf("=====TEST ONE=====\n");
 20     char dest1[5] = {0};
 21     char src1[10] = "abcdefghi";//abcdefghi\0
 22     myStrncpy(dest1, src1, sizeof(dest1));//in case of overflow
 23     int i = 0;
 24     for(; i < sizeof(dest1); i++)
 25     {
 26         printf("dest1[%d] = %d ", i, dest1[i]);
 27     }
 28     putchar(10);
 29     putchar(10);
 30     printf("=====TEST TWO=====\n");
 31     char dest2[10] = {0};
 32     char src2[5] = "abcd";//abcd\0
 33     for(i = 0; i < sizeof(dest2); i++)
 34     {   
 35         dest2[i] = 11;//give it an initial value
 36     }
 37     myStrncpy(dest2, src2, sizeof(dest2));//in case of overflow
 38     for(i = 0; i < sizeof(dest2); i++)
 39     {
 40         printf("dest2[%d] = %d ", i, dest2[i]);
 41     }
 42     putchar(10);
 43     return 0;
 44 }


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值