The function checks the string character by character. It finds each word, compares its length with the longest word so far, and prints the longest one.
#includevoid print_longest_word(const char* str) { int maxStart = 0; int maxLen = 0; int start = 0; int len = 0; int i = 0; while (str[i] != '\0') { if (str[i] != ' ') { if (len == 0) { start = i; } len++; } else { if (len > maxLen) { maxLen = len; maxStart = start; } len = 0; } i++; } if (len > maxLen) { maxLen = len; maxStart = start; } for (i = maxStart; i < maxStart + maxLen; i++) { printf("%c", str[i]); } printf("\n"); }