2015年12月2日 星期三

[C Language] strcpy 用法

strcpy and strncpy 是複製字串的指令,是在 string.h 中的function 所以要記得先include <string.h>,

方法是

strcpy(字串變數, 字串變數等於的值);
strncpy(字串變數, 字串變數等於的值, 擷取自串的長度);


exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    char s[] = "Well begun is half done.";
    char t[25];
    char t1[5];
    strcpy(t, s);
    strncpy(t1, t, 4); 
    printf("%s\n", t);
    printf("%s\n", t1);
    
 return 0;
}


print:
Well begun is half done.
Well



refs:http://pydoing.blogspot.tw/2010/07/c-strcpy.html