SHARE
TWEET

Untitled

a guest Sep 13th, 2017 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.     char* s = (char*) calloc(1, sizeof(char));
  7.     int ws = 0, we = 0, w_max = 0, ws_max = 0, count = 1, r;
  8.  
  9.     while ((r = getchar()) != EOF) {
  10.         s = (char*) realloc(s, count);
  11.         s[count - 1] = r;
  12.  
  13.         if (r == ' ' || r == '\n') {
  14.             we = count - 1; // -1 == '\0'
  15.  
  16.             if (we - ws > w_max) {
  17.                 w_max = we - ws;
  18.  
  19.                 ws_max = ws;
  20.             }
  21.  
  22.             ws = count;
  23.         }
  24.  
  25.         count += 1;
  26.     }
  27.  
  28.     char* word_start = &(*(s + ws_max));
  29.     *(s + ws_max + w_max) = '\0';
  30.     printf("%d characters in longest word: %s\n", w_max, word_start);
  31.     free(s);
  32.  
  33.     return 0;
  34. }
RAW Paste Data
Top