Guys can please help find a way on how to make my code robust. I am only allowed to accept string inputs. If I like input numbers, it would display an invalid input. Instead of continuing on the output.
This code is about getting five-string inputs and it displays the longest word.
Example output:
Here is the code:
#include <stdio.h>
#include <string.h>
int main() {
int wordSize[5], longestInt;
char longest[50];
char words[5][50];
char longestWord[50];
printf("\n");
for(int i=0; i < sizeof(words)/ sizeof(words[0]); i++) {
printf("Enter string %d: ", i+1);
gets(words[i]);
wordSize[i] = strlen(words[i]);
}
longestInt = wordSize[0];
for(int i=0; i < sizeof(words)/ sizeof(words[0]); i++) {
if(longestInt < strlen(words[i])) {
longestInt = wordSize[i];
}
}
if(longestInt == wordSize[0]) {
strcpy(longestWord, words[0]);
} else if(longestInt == wordSize[1]) {
strcpy(longestWord, words[1]);
} else if(longestInt == wordSize[2]) {
strcpy(longestWord, words[2]);
} else if(longestInt == wordSize[3]) {
strcpy(longestWord, words[3]);
} else if(longestInt == wordSize[4]) {
strcpy(longestWord, words[4]);
} else {
printf("Invalid");
}
printf("The longest word is: %s", longestWord);
printf("\n");
return 0;
}