第一种拷贝方法
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//字符串拷贝
//第一种的实现方式
void copyString01(char *dest, char *source)
{
//利用下标的方式进行copy
int len = strlen(source);
for (int i = 0; i < len; i++)
{
dest[i] = source[i];
}
dest[len] = '\0';
}
void test01()
{
char * str = "hello world";
char buf[1024];
//char buf[1024] = {0};
copyString01(buf, str);
printf("buf=%s\n", buf);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
第二种拷贝方法
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//字符串拷贝
//第一种的实现方式
void copyString01(char *dest, char *source)
{
//利用下标的方式进行copy
int len = strlen(source);
for (int i = 0; i < len; i++)
{
dest[i] = source[i];
}
dest[len] = '\0';
}
//第二种的实现方式 一个一个解引用
void copyString02(char *dest, char *source)
{
//利用字符串指针的方式进行拷贝
while (*source != '\0')
{
*dest = *source;
dest++;
source++;
}
*dest = '\0';
}
void test02()
{
char * str = "hello world";
char buf[1024];
//char buf[1024] = {0};
copyString02(buf, str);
printf("buf=%s\n", buf);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
第三种拷贝方法
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//字符串拷贝
//第一种的实现方式
void copyString01(char *dest, char *source)
{
//利用下标的方式进行copy
int len = strlen(source);
for (int i = 0; i < len; i++)
{
dest[i] = source[i];
}
dest[len] = '\0';
}
//第二种的实现方式 一个一个解引用
void copyString02(char *dest, char *source)
{
//利用字符串指针的方式进行拷贝
while (*source != '\0')
{
*dest = *source;
dest++;
source++;
}
*dest = '\0';
}
//第三种拷贝方式
void copyString03(char *dest, char *source)
{
while (*dest++ = *source++)
{
}
}
void test03()
{
char * str = "hello world";
char buf[1024];
//char buf[1024] = {0};
//copyString02(buf, str);
copyString03(buf, str);
printf("buf=%s\n", buf);
}
int main()
{
//test01();
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}