Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- int main(void)
- {
- char* s = (char*) calloc(1, sizeof(char));
- int ws = 0, we = 0, w_max = 0, ws_max = 0, count = 1, r;
- while ((r = getchar()) != EOF) {
- s = (char*) realloc(s, count);
- s[count - 1] = r;
- if (r == ' ' || r == '\n') {
- we = count - 1; // -1 == '\0'
- if (we - ws > w_max) {
- w_max = we - ws;
- ws_max = ws;
- }
- ws = count;
- }
- count += 1;
- }
- char* word_start = &(*(s + ws_max));
- *(s + ws_max + w_max) = '\0';
- printf("%d characters in longest word: %s\n", w_max, word_start);
- free(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement