This C program reads a line of text from keyboard and then writes the longest word in the line. The issue with my code below is that it only prints the last word apart from its length, although everything seems fine. Can anyone see a problem with my code?
#include<stdio.h>
#include<string.h>
#define MAX 132
#define MAXW 30
int Len_w[MAXW];
int Max_V(int vf[], int len);
main()
{
char s[MAX+1], w[MAXW], *Ind_w[MAXW],*p,out[MAXW];
int k=0, i=0, Maximum, g=0;
printf("\nInsert the line....\n");
p=fgets(s, MAX, stdin);
while(sscanf(p, "%s%n", w, &k)==1){
Len_w[i] = strlen(w);
Ind_w[i] = w; //the issue is here!!
p+=k+1;
i++;
}
Maximum = Max_V(Len_w,i);
for(g=0;g<i;g++){
if(Len_w[g] == Maximum){
//sscanf(Ind_w[g],"%s",out);
printf("\n%s", Ind_w[g]);
}
}
return 0;
}
/*----------------------------------------------------------------------------*/
int Max_V(int vf[], int len)
{
int j; int Max;
Max=*vf;
for(j=1; j < len; j++)
{
if(*(vf+j) > Max)
{
Max=*(vf + j);
}
}
return Max;
}
/*----------------------------------------------------------------------------*/