C Primer Plus 第11章 11.13 编程练习答案
1、设计并测试一个函数,可以从输入读取n个字符(包括空格、制表符和换行符),把结果存储在一个数组中,这个数组的地址通过参数来传递。 #include<stdio.h> void input(char *p,int n); int main(void) { char a[81]; int n; puts("Input the number of the string."); scanf("%d",&n); getchar(); /*滤去回车*/ puts("Input the string."); input(a,n); puts(a); return 0; } void input(char *p,int n) { int i; for(i=0;i<n;i++) *(p+i)=getchar(); *(p+i)='\0'; } 2、修改并测试练习1中的函数,使得可以在n个字符后,或第一个空格、制表符、换行符后停止读取输入,由上述情况中最先被满足的那个终止读取(不能使用scanf()函数)。 #include<stdio.h> #include<ctype.h> void input(char *p,int n); int main(void) { char a[81]; int n; puts("Input the number of the string."); scanf("