puzzles/longest-word/longestword.c

43 lines
711 B
C
Raw Normal View History

2024-04-02 21:01:09 +00:00
#include <stdio.h>
int count_letters(char *str, char *word) {
char c;
int letters = 0;
while ((c = *str++) && *word) {
if (c == *word) {
letters++;
word++;
}
}
if (!*word) {
return letters;
} else {
return 0;
}
}
int main(int argc, char **argv) {
if (argc < 2) {
return 1;
}
char *word;
int max_letters = 0;
for (int i = 2; i < argc; i++) {
int letters = count_letters(argv[1], argv[i]);
if (letters > max_letters) {
word = argv[i];
max_letters = letters;
}
}
if (max_letters > 0) {
printf("%s\n", word);
}
return 0;
}