#include
#include
int getline(char line[], int max)
{
int nch = 0, c;
max = max - 1; /* leave room for '\0' */
while ((c = getchar()) != EOF)
{
if (c == '\n') break;
if (nch < max)
{
line[nch] = c;
nch++;
}
}
if (c == EOF && nch == 0)
return EOF;
line[nch] = '\0';
return nch;
}
int main()
{
char * line;
int linelen;
printf("Specify the sentence length:\n");
scanf("%d", &linelen);
if (linelen <= 0)
{
printf("ERROR: line length must be positive!\n");
return 1;
}
line = (char *) calloc(linelen, sizeof(char));
//line = (char *) malloc(linelen);
if (line == NULL)
{
printf("ERROR: out of memory!\n");
return 1;
}
flushall();
printf("\nPlease type your sentence:\n");
getline(line, linelen);
printf("\nYou typed:\n");
printf("%s\n", line);
return 0;
}
No comments:
Post a Comment