Guest User

Untitled

a guest
Jan 12th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. void add_symbol(char **line, char symb, int len)
  6. {
  7.     int new_size = (len+2)*sizeof(char); //+1 for symbol, +2 for \0
  8.     char *line_copy = (char*) malloc(new_size);
  9.     strcpy(line_copy, *line);
  10.     line_copy[len] = symb;
  11.     line_copy[len+1] = '\0';
  12.     *line = (char *) realloc(*line, new_size);
  13.     strcpy(*line, line_copy);
  14.     free(line_copy);
  15. }
  16. int main()
  17. {
  18.     char input;
  19.     char *final_line = (char*) malloc(sizeof(char)*1);
  20.     final_line[0] = '\0';
  21.     while ((input = getchar()) != '0') //input != '0'
  22.     {
  23.         if (isalpha(input))
  24.             add_symbol(&final_line, input, strlen(final_line));
  25.         else if (input != '\n') //\n, \r and other != EOF
  26.             printf("Invalid input: %c\n", input); //
  27.     }
  28.     printf("Final line: \"%s\"\n", final_line);
  29.     free(final_line);
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment