Skip to content

Instantly share code, notes, and snippets.

@YoukaiCat
Created November 26, 2013 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoukaiCat/7664700 to your computer and use it in GitHub Desktop.
Save YoukaiCat/7664700 to your computer and use it in GitHub Desktop.
Ввод строк неопределенного размера.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFF_SIZE 6 /* 5 chars and \0 */
int main()
{
int count = 0;
char temp[BUFF_SIZE];
char * result = NULL;
printf("Please, enter a long string.\n> ");
do {
fgets(temp, BUFF_SIZE, stdin);
count++;
result = (char *) realloc(result, count * sizeof(char) * BUFF_SIZE);
if (result != NULL)
strcat(result, temp);
else
return 1;
} while(strlen(temp) == BUFF_SIZE-1 && temp[4] != '\n');
printf("String is: %s", result);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main()
{
int count = 0;
char c;
char * result = '\0';
printf("Please, enter a long string.\n> ");
while ((c = getchar()) != '\n')
{
count++;
result = (char *) realloc(result, count * sizeof(char));
if (result != NULL)
result[count - 1] = c;
else
return 1;
}
printf("String is: %s\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment