sicily 6766. tmp

1003. tmp

Description
Implement the following function, which copy string from b to a
void my_strcpy(char* a, char* b)

Input
none

Output
none

Hint

#include <string.h>
#include <stdio.h>

int main()
{
 char str_a[100] = "123456";
 char str_b[100] = "123456";
 
 my_strcpy(str_a + 1, str_a );

 if( strcmp(str_b, str_a+1) == 0 ){
  printf("YES\n");
 }
 else{
  printf("NO\n");
 }

 return 0;
}

从名字坑爹到题目内容的一题,跟玩猜谜一样……

意思就是要你写一个拷贝字符串的函数,用hint里那个主函数测试,可以输出YES的话交上去就能过(大概)。因为hint的那个主函数测试用例比较特别,是把字符串str_a拷贝一遍,然后从从第二个元素开始覆盖过去,也就是拷贝完str_a其实是1123456,所以在拷贝函数里不能直接用指针把b的值赋给a然后一起移动就算了,需要开一个中间数组临时存放拷贝的值,不然最后拷贝出的字符串会全被拷贝源最初指向的的字符覆盖。如果拷贝成功,把b和str_a+1用strcmp函数比较的话返回值就是0,说明两个字符串长度相同(因为str_a是从str_a+1,就是第二个元素开始数起的)

View Code
 1 void my_strcpy( char *a, char *b )
 2 {
 3     int i;
 4     char temp[100] = {0};
 5     char *ptr = temp;
 6     
 7     for ( i = 0; *(b + i) != '\0'; i++ )
 8     {
 9         *(ptr + i) = *(b + i);
10     }
11     
12     for ( i = 0; *(ptr + i) != '\0'; i++ )
13     {
14         *(a + i) = *(ptr + i);
15     }
16     
17     return;
18 }
原文地址:https://www.cnblogs.com/joyeecheung/p/2813698.html