Dont take my word. try diffrent inputs like
> 1
> enter
> 1234 12
> 1234 12 12
> abc 123
Are they all working ????
Code:
int main(void)
{
char s[100];
int a[5], length, i, j;
/* initalize the array in case the user is lazy
and just press enter */
for( i = 0 ; i < 5 ; i++)
a[i] = 0;
printf("Enter the text : ");
fgets(s, sizeof(s), stdin); /* it adds '\n' to the string */
/* scanf("%s",s); */
i = j = length=0;
while(s[i]!='\0')
{
if(s[i] == ' ') /* should be '==' and not '=' */
i++;
else
{ /* should be in parthince. */
while( s[i] && s[i]!=' ') /* check for overflow in s */
{
length++;
i++;
}
a[j]=length;
j++;
if ( j >= 5 ) /* check for overflow in a */
break;
length=0;
}
}
for(i=0 ; i < 5 ; i++)
printf("Word %d : %d characters\n",i+1,a[i]);
return(0);
}