Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- void add_symbol(char **line, char symb, int len)
- {
- int new_size = (len+2)*sizeof(char); //+1 for symbol, +2 for \0
- char *line_copy = (char*) malloc(new_size);
- strcpy(line_copy, *line);
- line_copy[len] = symb;
- line_copy[len+1] = '\0';
- *line = (char *) realloc(*line, new_size);
- strcpy(*line, line_copy);
- free(line_copy);
- }
- int main()
- {
- char input;
- char *final_line = (char*) malloc(sizeof(char)*1);
- final_line[0] = '\0';
- while ((input = getchar()) != '0') //input != '0'
- {
- if (isalpha(input))
- add_symbol(&final_line, input, strlen(final_line));
- else if (input != '\n') //\n, \r and other != EOF
- printf("Invalid input: %c\n", input); //
- }
- printf("Final line: \"%s\"\n", final_line);
- free(final_line);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment