Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h> /* for memory allocation functions */
  3. #include <ctype.h> /* for isalnum function */
  4.  
  5. #define LINE 80 /* Characters in each line */
  6. #define MEM_SIZE 100 /* Initial memory size */
  7.  
  8. /*gets a char input with an undefined size, counting each char and checking if it's an alpha-numeric char */
  9. char *getInput(int *counter, int *counteran);
  10. /*prints the user input in a symmetric way*/
  11. void printInput(char *input);
  12.  
  13. /* This program reads user input with an undefined size, store it in memory according to its size, and prints input in a symmetric way
  14.     then, it prints the total number of char in input, and the number of alpha-numeric chars. */
  15.  
  16. int main()
  17. {
  18.     char *mainInput; /* pointer to the dynamic array, will later be initalized */
  19.     int cnt = 0;     /* characters counter */
  20.     int cntan = 0;   /* alpha-numeric characters counter */
  21.     printf("Please enter your text:\n");
  22.     mainInput = getInput(&cnt, &cntan); /* sending addresses then storing in them the processed data */
  23.     if (mainInput == NULL)
  24.     {
  25.         return 1;
  26.     }
  27.  
  28.     printf("\n");
  29.     printf("------------------------------ OUTPUT ------------------------------\n\n");
  30.     printInput(mainInput);
  31.     printf("\n");
  32.     printf("Total Characters: %d\n", cnt);
  33.     printf("Total Alpha-Numeric Characters: %d\n",cntan);
  34.     free(mainInput);
  35.     return 0;
  36. }
  37.  
  38. char *getInput(int *counter, int *counterAN)
  39. {
  40.     int cntan = 0;
  41.     char *p = (char *)malloc(MEM_SIZE * sizeof(char));
  42.     char *q = p; /*backup pointer, if realloc fails to allocate, function will return last address values stored*/
  43.     int c;
  44.     long current = 0, last = MEM_SIZE - 1;
  45.     while ((c = getchar()) != EOF)
  46.     {
  47.         if (current >= last) /* if reached last bit */
  48.         {
  49.             q = p;
  50.             p = (char *)realloc(q, last + (MEM_SIZE * sizeof(char))); /* attempting to add more bits */
  51.             if (p == NULL)
  52.             {
  53.                 printf("Memory allocation failed, printing only stored values \n");
  54.                 return q;
  55.             }
  56.             last += MEM_SIZE;
  57.         }
  58.  
  59.         p[current] = c;
  60.         ++current;
  61.         if (isalnum(c)) /* checking if current char is alpha-numeric */
  62.             cntan++;
  63.     }
  64.     p[current] = '\0';
  65.     (*counter) = current; /* index equals to number of charaters, transfering it through address */
  66.     (*counterAN) = cntan; /* transfering value through address */
  67.     return p;
  68. }
  69.  
  70. void printInput(char *input)
  71. {
  72.     int line_index = 0, i = 0;
  73.     while (input[i] != '\0') /* until not reached end of input */
  74.     {
  75.         while (line_index < LINE && (input[i] != '\0')) /* printing lines in a symmetric way */
  76.         {
  77.             line_index++;
  78.             putchar(input[i]);
  79.             i++;
  80.         }
  81.         line_index = 0;
  82.         putchar('\n');
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement