题解 2 复制部分字符串
复制部分字符串
https://www.nowcoder.com/practice/8f5b923683b94e549880e3c8370e3e55
#include <stdio.h>
int main() {
char str[30] = { 0 };
gets(str);
int m;
scanf("%d",&m);
// write your code here......
// 1.定义一个新数组来存放复制后的字符串
char new_str[30] = {0};
// 2.定义一个源指针
char *p_src = str +(m-1);
char *p_dest = new_str;
while(*p_src != '\0')
{
*p_dest = *p_src;
p_src++;
p_dest++;
}
// 5. 当循环结束时,p_src 指向了 '\0'
// 我们需要为新字符串 new_str 也添加一个末尾的 '\0'
*p_dest = '\0';
printf("%s\n",new_str);
return 0;
}
查看17道真题和解析