题解 | #复制部分字符串#
复制部分字符串
https://www.nowcoder.com/practice/8f5b923683b94e549880e3c8370e3e55
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char str[30] = { 0 };
cin.getline(str, sizeof(str));
int m;
cin >> m;
// write your code here......
char result[30] = {0};
//做俩判断 m不能是负数也不能>=helloworld本身
if (m >= strlen(str))
{
printf("wrong!");
}
else if (m < 0)
{
printf("wrong!");
}
else
{
// 如果不搞(m-1)的话,输入是orld而不是world.
strncpy(result, str + (m - 1), strlen(str) - (m - 1));
printf("%s", result);
}
return 0;
}
查看20道真题和解析