求讲解这道题 谢谢
题干:
设计并测试一个函数,其功能是读取输入行里的第一个单词到数组,并丢掉该行中其他的字符。一个单词的定义是一串字符,其中不含空格、制表符和换行符。
这个是我写的:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void input(char *p)
{
int i;
puts("输入字符串");
fgets(p,81,stdin);
for(i=0;i<81;i++)
if(isspace(p[i]))
p[i] = '\0';
fputs(p,stdout);
}
int main(void)
{
char ar[81];
int i;
input(ar);
return 0;
}
这个是标准答案:
#include <stdio.h>
#include <ctype.h>
void word(char *p);
int main(void)
{
char a[81];
puts("input your string:");
gets(a);
word(a);
puts(a);
return 0;
}
void word(char *p)
{
int begin,end;
for(begin=0; isspace( *(p+begin) ) ;begin++) continue;
for(end=begin; !isspace( *(p+end) ) ;end++) continue;
*(p+end) = '\0';
for(; *(p + begin) != '\0'; p++)
*p = *(p + begin);
*p = '\0';
}
但是2个程序我拿到IDE上都不能通过编译。不知道问题出在哪。希望能讲解下。谢谢