Description
输入一个整型数,然后申请对应大小空间内存,然后读取一个字符串(测试用例的字符串中含有空格),字符串的输入长度小于最初输入的整型数大小,最后输出输入的字符串即可(无需考虑输入的字符串过长,超过了内存大小);
注意下面问题:
char *p;
scanf("%d",&n);
p=malloc(n);
scanf("%c",&c);//注意在scanf和gets中间使用scanf("%c",&c),去除换行
fgets(p,n,stdin);
注意:OJ不支持fflush(stdin)清空标准输入缓冲区操作。
OJ不支持gets,因为C11标准去掉了,部分学校机试可以用gets,部分不可以,因此建议使用fgets
Input
一个整型数和一个字符串,例如
10
hello
Output
输出输入的字符串,上面输入的是hello,那么输出hello
Sample Input 1
10
hello
Sample Output 1
hello
Code
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
int i;
char *p,c;
scanf("%d ",&i);//此处%d后加个空格
// scanf("%d",&i);
// scanf("%c",&c);//或者此处使用scanf("%c",&c)清除换行
p = (char *)malloc(i);
// gets(p);
fgets(p,i,stdin);
puts(p);
free(p);
return 0;
}
fgets()
#include <stdio.h>
char * fgets(char * s, int n,FILE *stream);
其中
s是字符型指针,指向存储读入数据的缓冲区的地址;n是从流中读入n-1个字符;sream是指向读取的流。当stream为stdin是指的标准输入流,即键盘输入到缓冲区。