编写一个函数,将字符串读入到数组中

编写一个函数,将字符串读入到数组中

在这里插入代码片
#include
#include
#include

char* get_string(char* a, int size){
	printf("请输入一串字符串:");
	fgets(a, size, stdin);
	if(a[strlen(a)- 1] == '\n'){
		a[strlen(a) - 1] = '\0';
	}else{
		char ch = '\0';
		while((ch = getchar()) != '\0' && ch != EOF);
	}
	return a;
}
int main(){
	char a[20] = {'\0'};
	char* num = get_string(a, 20);
	printf("%s",num);
	return 0;
}

你可能感兴趣的:(常用函数功能)