strcpy()
函数、strcat()
函数、strcmp()
函数是长度不受限制的字符串函数,存在危险。strncpy()
函数、strncat()
函数、strncmp()
函数多了一个参数n
,限制了对字符串的访问,相对来说安全一些。
点击转到cpluscplus.com官网 - strncpy
所需头文件为<string.h>
num
个字符从源字符串到目标空间。num
,在拷贝完源字符串之后,在目标的后面追加0,直到num
个。source
的长度大于num
,则不会在destination
的末尾隐式添加空字符。因此,在这种情况下,destination
不应被视为一个以空结尾的C
字符串(这样读取它会溢出)。//模拟实现strncpy
#include <stdio.h>
#include <assert.h>
char* my_strncpy(char* destination, const char* source, size_t num) {
//断言,指针destination与指针source接受的均不是NULL
assert(destination && source);
//记录目标空间地址
char* start = destination;
while (num-- && (*destination++ = *source++)) {
;
}
num++;
destination--;
while (num--) {
*destination++ = '\0';
}
return start;
}
int main() {
char str1[] = "ab";
char str2[20] = "************";
char* p = my_strncpy(str2, str1, 2);
puts(p);
return 0;
}
运行结果:
vs2019给出的实现:
char * __cdecl strncpy (
char * dest,
const char * source,
size_t count
)
{
char *start = dest;
while (count && (*dest++ = *source++)) /* copy string */
count--;
if (count) /* pad out with zeroes */
while (--count)
*dest++ = '\0';
return(start);
}
点击转到cpluscplus.com官网 - strncat
所需头文件为<string.h>
num
个元素追加到目标空间的后面。num
,则只追加源字符串本身,到达源字符串的末尾后不在目标空间补充'\0'
。num
,再追加完源字符串的num
个元素后,会为目标空间补上'\0'
。#include <stdio.h>
#include <assert.h>
char* my_strncat(char* destination, const char* source, size_t num) {
//断言
assert(destination && source);
//
char* start = destination;
//
while (*destination) {
destination++;
}
//
while (num && (*destination++ = *source++)) {
num--;
}
if (!num) {
*destination = '\0';
}
return start;
}
int main() {
char str1[] = "word!";
char str2[20] = "Hello \0**********";
my_strncat(str2, str1, 9);
return 0;
}
运行结果:
vs2019给出的实现:
char * __cdecl strncat (
char * front,
const char * back,
size_t count
)
{
char *start = front;
while (*front++)
;
front--;
while (count--)
if (!(*front++ = *back++))
return(start);
*front = '\0';
return(start);
}
点击转到cpluscplus.com官网 - strncmp
所需头文件为<string.h>
num
个字符。num
个字符比较完。Return Value
Returns an integral value indicating the relationship between the strings:
return value | indicates |
---|---|
<0 | **the first character that does not match has a lower value in str1 than in **str2 |
0 | the contents of both strings are equal |
>0 | **the first character that does not match has a greater value in str1 than in **str2 |
#include <stdio.h>
#include <assert.h>
#include <string.h>
int my_strncmp(const char* str1, const char* str2, size_t num) {
//断言
assert(str1 && str2);
while (*str1 == *str2 && num) {
if (*str1 == '\0') {
return 0;
}
str1++;
str2++;
num--;
}
if (num) {
return *str1 - *str2;
}
else {
return 0;
}
}
int main() {
char str1[] = "abcdef";
char str2[] = "aca";
int ret = my_strncmp(str1, str2, 3);
return 0;
}
运行结果:
vs2019给出的实现:
int __cdecl strncmp
(
const char *first,
const char *last,
size_t count
)
{
size_t x = 0;
if (!count)
{
return 0;
}
/*
* This explicit guard needed to deal correctly with boundary
* cases: strings shorter than 4 bytes and strings longer than
* UINT_MAX-4 bytes .
*/
if( count >= 4 )
{
/* unroll by four */
for (; x < count-4; x+=4)
{
first+=4;
last +=4;
if (*(first-4) == 0 || *(first-4) != *(last-4))
{
return(*(unsigned char *)(first-4) - *(unsigned char *)(last-4));
}
if (*(first-3) == 0 || *(first-3) != *(last-3))
{
return(*(unsigned char *)(first-3) - *(unsigned char *)(last-3));
}
if (*(first-2) == 0 || *(first-2) != *(last-2))
{
return(*(unsigned char *)(first-2) - *(unsigned char *)(last-2));
}
if (*(first-1) == 0 || *(first-1) != *(last-1))
{
return(*(unsigned char *)(first-1) - *(unsigned char *)(last-1));
}
}
}
/* residual loop */
for (; x < count; x++)
{
if (*first == 0 || *first != *last)
{
return(*(unsigned char *)first - *(unsigned char *)last);
}
first+=1;
last+=1;
}
return 0;
}
END