How to dynamically accept a string with spaces? I had tried gets(), but it is not working, and I don't want to take input from file. I want to accept a string without specifying string size.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *accept()
{
char *s;
s = (char *)malloc(sizeof(char));
gets(s);
return s;
}
int main()
{
char *str;
printf("\nEnter`enter code here` one string");
str = accept();
}
callocif you want to read strings without specifying size. Do not usegets, instead usefgets.gets()is too dangerous to be used. Seriously look at POSIX functiongetline().accept()is not a good name to use — because there is a system functionaccept()used by networking code (servers, primarily).