strcpy 字符串拷贝

函数原型:char * strcpy(char * destination, const char *source);

  1. Copies the C string pointed by source into the array pointed by destination, including terminating null character (and stopping at that point).
  2. 源字符串必须以’\0’结束。
  3. 会将源字符串中的’\0’拷贝到目标空间。
  4. 目标空间必须足够大,以确保能存放源字符串。
  5. 目标空间必须可变,即为数组。

模拟实现strcpy函数,如下:

char* my_strcpy(char * destination, const char * source)
{
    char* p = destinaton;
    assert(destination && source);
    while(*destination++ = *source++)
    {
        ;
    }
    return p;     
}

strcat 字符串追加

函数原型:char* strcat(char* destination, const char* source);

  1. Appends a copy the source string to the destination string. The terminating null in destination is overwritten by the first character of source, and a null-character is at the end of the new string formed by the concatenation of both in destination.
  2. 源字符串必须以’\0’结束
  3. 目标空间必须足够大,以确保能存放源字符串。
  4. 目标空间必须可变,即为数组。

使用方法如下:

int main()
{
    char arr[20] = "hello ";
    strcat(arr, "world");
    printf("%s\n", arr);
}

打印结果如下:

hello world

模拟实现strcat, 如下所示:

char * my_strcat(char* dest, const char* src)
{
    assert(dest && src);
    char* p = dest;
    while(*dest != '\0')
    {
        dest++;
    }
    while(*dest++ = *src++)
    {
        ;
    }
    return p;
}

strcmp 字符串大小比较

函数原型:int strcmp(const char* str1, const char* str2);

  1. This function starts comparing the first character of each string. If they are equal to other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
  2. 第一个字符串大于第二个字符串,则返回大于0的数字。
  3. 第一个字符串等于第二个字符串,则返回0。
  4. 第一个字符串校园第二个字符串,则返回小于0的数字。

int main()
{
	char arr1[20] = "zhangsan";
	char arr2[] = "zhangsanfeng";
	int ret = strcmp(arr1, arr2);

	if (ret < 0)
	{
		printf("<\n");
	}
	else if (ret == 0)
	{
		printf("==\n");
	}
	else
	{
		printf(">\n");
	}
}

模拟strcmp实现,如下:

int my_strcmp(const char* str1, const char*str2)
{
    assert(str1 && str2);
    while(*str1 == *str2)
    {
        if (*str1 ==  '\0')
            return 0;  //字符串相等
        str1++;
        str2++;
    }
 
   // if (*str1 > *str2)
        //  return 1;
   // else
       //   return -1;
    
     return (*str1 - *str2);
}

#strncpy 拷贝指定数量的字符
函数原型:char * strncpy(char * destination, const char * source, size_t num);

  1. 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 writeen to it.

int main()
{
	char arr1[20] = "abcdef";
	char arr2[] = "hello world";
	char arr3[] = "hello";

	strncpy(arr1, arr2, 5);
	printf("%s\n", arr1);

	strncpy(arr1, arr3, 8);   // arr3字符串串长度小于8,则不够的用\0补上
	printf("%s\n", arr1);
}

打印结果如下:

hellof
hello

strncat 追加指定数量的字符串

函数原型:char * strncat(char * destination, const char * source , size_t num);

Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

int main()
{
    char arr1[20] = "hello ";
    char arr2[] = "world";
    strncat(arr1, arr2, 3);     //在追加的末尾加上'\0'
    printf("%s\n", arr1);
}

打印结果:

hello wor

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部