C primer plus 第六版 第十一章 第二题 编程练习答案

版权声明:转载请注明来源~ https://blog.csdn.net/Lth_1571138383/article/details/85121236

Github地址:φ(>ω<*)这里这里。
 

/*
    本次任务为修改任务1的函数,并在n个字符后停止,或遇到空白字符时停止,哪个先遇到哪个停止。
                不能只使用scanf()。
*/
 

#include<stdio.h>
#include<string.h>

#define n 100

void get_you(char chr[n], int limt);

int main(void)
{
	
	char chrs[n];

	printf("Now, Please input(Limit character is 100):\n");
	get_you(chrs, n);

	putchar('\n');
	puts(chrs);
	putchar('\n');

	getchar();
	return 0;
}

void get_you(char chr[n], int limt)
{
	int i = 0;

	while( (chr[i] = getchar() ) != NULL )
	{
		if(i > limt || chr[i] == '\n' || chr[i] == ' ')
		{
			chr[i] = '\0'; 

			fflush(stdin);

			break; 
		}

		i++;
	}

	return;
}

猜你喜欢

转载自blog.csdn.net/Lth_1571138383/article/details/85121236