Given any number of program parameters input into the command-line, calculate the length of each one, and lastly output the longest string. Here is my code, but it seems to be wrong.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
size_t maxlen = 0, len;
int i;
int longest;
for (i = 1; i < argc; i++) {
len = strlen(argv[i]);
if (len > maxlen)
longest = argv[i];
}
printf("The longest string is %s. \n", longest);
return 0;
}
Mistakes:
One.
char max; ... max = strlen(argv[i]);wrong;strlen()returnssize_tand not achar.Two:
if(max < argv[i])also wrong, you’re comparing the length of the string with a pointer to the string. That doesn’t even make sense. What you probably want is