<c primer plus>第 11 章第 3 练习,自己做的没参考答案,显示垃圾值!求指点!!
<c primer plus>第11章第3练习,自己做的没参考答案,显示垃圾值!要求读取输入行里第一个单词到数组,并丢掉其他的字符。感觉指针的用法没有错,但是结果不如意,求指点!程序如下:
程序代码:#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
void getword(char *str);
int main(void)
{
char input[81];
puts("Enter your str:\n");
getword(input);
puts("The First str is:\n");
puts(input);
return 0;
}
void getword(char *str)
{
char c;
int i=1;
bool inword=false;
while((c=getchar())!='\n')
{
if(!isspace(c)&&!inword)
{
*str=c;
inword=false;
}
else if(!isspace(c)&&inword)
{
*(str+i++)=c;
}
else
break;
}
}
程序结果如下:
