Created
November 26, 2013 19:34
-
-
Save YoukaiCat/7664700 to your computer and use it in GitHub Desktop.
Ввод строк неопределенного размера.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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