
C语言字符串操作函数总结
Part 1: Copying
1、 函数名: strcpy
用 法: char * strcpy ( char * destination, const char * source );
功 能: 串拷贝(Copies the C string pointed by source into the array pointed by destination, including the terminating null character. To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.)
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%s/n", string);
return 0;
}
2、函数名: strncpy
用 法: char * strncpy ( char * destination, const char * source, size_t num );
功 能: 指定长度的串拷(Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. No null-character is implicitly appended to the end of destination, so destination will only be null-terminated if the length of the C string in source is less than num.)
程序例:
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);
str2[5]='/0';//no '/0' will be appendeded to the end of destination automatically
puts (str2);//输出To be
return 0;
}
另外参考:
void * memcpy ( void * destination, const void * source, size_t num );
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33666-1.html
果断要找一个会踢球的老公呀~
期待你带给我们更多更好的作品