Longest Word
Completed
Read n words and print the longest (first on ties) and its length.
Input
n, then n words.
Output
One line: word length
Example
Input
5 the quick brown fox jumps
Output
quick 5
Editor
C
Hints
Start with an empty best string and copy any longer word into it.
Solution explanation
Strings must be copied with strcpy; assigning arrays isn't allowed in C.
Show solution
#include <stdio.h>
#include <string.h>
int main() {
int n;
char w[100], best[100];
scanf("%d", &n);
best[0] = '\0';
for (int i = 0; i < n; i++) {
scanf("%s", w);
if (strlen(w) > strlen(best)) strcpy(best, w);
}
printf("%s %d\n", best, (int) strlen(best));
return 0;
}