编写函数disp(char *s , char *output , int n),实现从s指向的字符串中提取前n个字符,若字符串中不够n个字符,
则提取到字符串结束符为止,将提取的字符串拷贝(不能用字符串拷贝库函数)到output数组中。再编写一个主函数,
从键盘输入一个字符串src,调用函数disp(),将src字符串中的第4到第10个之间的字符串提取到out数组(在主函数
中定义)中并显示输出。
我是初学者,可以写的简单一点吗?就用C语言
追答这就是是C语言,没法再简化了。
追问编译的时候有错误,我看不懂,不会改.....
追答你要是用 Visual Studio,
char out[out_length + 1]; 改成 char* out = (char*)malloc(out_length + 1);
return前加free(out)
E:\C\src.c(11) : error C2143: syntax error : missing ';' before 'type'
E:\C\src.c(12) : error C2065: 'lens' : undeclared identifier
执行 cl.exe 时出错.
把函数写成这样吧,你试试,反正也不需要判断
void disp(char *s , char *output , int n)
{
int lens = strlen(s);
if(lens < 10)
{
strcpy(output, s);
}
else
{
strncpy(output, s, 10);
}
}