《c primer plus》里面第 11 章的课后练习题第 2 题,自己编的感觉没错啊,为什么没有效果!!
《c primer plus》里面第11章字符串与字符串函数的课后练习题第2题,要求n个字符后遇到第一个空格、制表符、换行符后停止读取输入。参考答案用的是for()里嵌套if(),自己练习用while()。看好几遍了感觉没有错误,为什么输入空格、制表符、换行符后不能停止?求指点!!!!
程序代码:#include<stdio.h>
#include<ctype.h>
void input(char *,int);
int main(void)
{
char a[81];
int n;
puts("Enter the number of you want:");
scanf("%d",&n);
getchar();
input(a,n);
puts(a);
return 0;
}
void input(char *str,int n)
{
int i=0;
puts("Enter str:");
while(isspace(*(str+i))==NULL||i<n)
{
*(str+i)=getchar();
i++;
}
*(str+i)='\0';
}
运行结果









