Advertisement
Guest User

Poor implementation in C

a guest
Sep 10th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //custom getline method, it sucks but oh well, live with it
  5. char* getline(){
  6.     int ch, i = 0;
  7.     char *toWorkWith, *temp;
  8.     toWorkWith = (char*) malloc(sizeof(char) + 1);
  9.     while ((ch = getchar()) && ch != '\n' && ch != '\0'){
  10.         toWorkWith[i] = ch;
  11.         i++;
  12.         temp = (char*) realloc(toWorkWith, ((i + 3) * sizeof(char)));
  13.         if (temp != NULL){
  14.             toWorkWith = temp;
  15.         }
  16.         else{
  17.             fprintf(stderr, "out of memory!");
  18.         }
  19.     }
  20.     toWorkWith[i] = '\0';
  21.     return toWorkWith;
  22. }
  23.  
  24. int main(int argc, char* argv[]){
  25.     int i, j, k, count;
  26.     char *flags, temp = 0;
  27.     char **words;
  28.  
  29.     count = atoi(getline());
  30.    
  31.     words = malloc(count * sizeof(char));
  32.     flags = malloc(count * sizeof(char));
  33.  
  34.  
  35.     for (i = 0; i < count; i++){
  36.  
  37.         printf("Loop %d of %d\n", i, count);
  38.         words[i] = getline();
  39.         flags[i] = 0;
  40.     }
  41.  
  42.     for (i = 0; i < 256; i++){
  43.  
  44.         for (j = 0; j < count; j++){
  45.            
  46.             if (words[j][i] == '\0'){
  47.                 flags[j] = 1;
  48.             }
  49.  
  50.             if (flags[j]){
  51.                 printf(" ");
  52.             }
  53.             else{
  54.                 printf("%c", words[j][i]);
  55.             }
  56.         }
  57.         printf("\n");
  58.  
  59.         for (k = 0, temp = 0; k < count; k++){
  60.             temp += flags[k];
  61.         }
  62.  
  63.         if (temp == count){
  64.             break;
  65.         }
  66.  
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement