大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
答: strcpy函数用于字符串复制, 也就是说将源地址(src)里面的内容复制到目的地址(dst)指向的位置,自然dst和src的地址不能是同一个,不然的话就地复制也没有意义。希望能够帮助到你。
成都创新互联自2013年起,先为海盐等服务建站,海盐等地企业,进行企业商务咨询服务。为海盐企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
#include stdio.h
/*比较两个字符串*/
/*src、dst相等 return 1*/
/*src比dst短 return -2*/
/*src比dst长 return 2*/
/*src、dst长度一致但不相等 return -1*/
int stringCmp (char *src, char *dst) {
if (strlen (src) strlen (dst))
return -2;
else if (strlen (src) strlen (dst))
return 2;
else {
while (*src *dst) {
if (*src != *dst)
return -1;
src++;
dst++;
}
return 1;
}
}
/*合并两个字符串*/
void mergeString(char* str1, char* str2, char* merge) {
while (*merge++ = *str1++); /*复制str1并将指针移动到字符串的末尾的下一个位置*/
merge--; /*退到字符串的末尾*/
while (*merge++ = *str2++); /*从末尾(\0)的位置开始将str2拷贝进来*/
*merge = '\0';
}
int main(void) {
char *str1_1="hel",*str1_2="lo", *merge1;
char *str2_1="ness",*str2_2="happy", *merge2;
char *str3_1="hi",*str3_2="hi", *merge3;
char *str4_1="hello",*str4_2="world", *merge4;
puts ("第1组字符串:");
puts (str1_1);
puts (str1_2);
if (stringCmp (str1_1, str1_2) == 1)
puts (str1_1);
else if (stringCmp (str1_1, str1_2) == -2) {
mergeString (str1_2, str1_1, merge1);
puts (merge1);
}
else if (stringCmp (str1_1, str1_2) == 2 || stringCmp (str1_1, str1_2) == -1) {
mergeString (str1_1, str1_2, merge1);
puts (merge1);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第2组字符串:");
puts (str2_1);
puts (str2_2);
if (stringCmp (str2_1, str2_2) == 1)
puts (str2_1);
else if (stringCmp (str2_1, str2_2) == -2) {
mergeString (str2_2, str2_1, merge2);
puts (merge2);
}
else if (stringCmp (str2_1, str2_2) == 2 || stringCmp (str2_1, str2_2) == -1) {
mergeString (str2_1, str2_2, merge2);
puts (merge2);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第3组字符串:");
puts (str3_1);
puts (str3_2);
if (stringCmp (str3_1, str3_2) == 1)
puts (str3_1);
else if (stringCmp (str3_1, str3_2) == -2) {
mergeString (str3_2, str3_1, merge3);
puts (merge3);
}
else if (stringCmp (str3_1, str3_2) == 2 || stringCmp (str3_1, str3_2) == -1) {
mergeString (str3_1, str3_2, merge3);
puts (merge3);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第4组字符串:");
puts (str4_1);
puts (str4_2);
if (stringCmp (str4_1, str4_2) == 1)
puts (str4_1);
else if (stringCmp (str4_1, str4_2) == -2) {
mergeString (str4_2, str4_1, merge4);
puts (merge4);
}
else if (stringCmp (str4_1, str4_2) == 2 || stringCmp (str4_1, str4_2) == -1) {
mergeString (str4_1, str4_2, merge4);
puts (merge4);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
getch (); /*屏幕暂留*/
return 0;
}
运行结果
这个函数的参数是指针,此时是按地址传递的,函数内的修改就能带出来(按值传递不行)
dst是一个操作的指针,最后落在字符串的结束标记那里,返回了也没用。
而str存的是字符串的首地址。