i need help regarding determining the longest word of a string..we would enter a sentence and the program would display the longest word/s i have read ideas about the strtok but i cant seem to understand it that much..and we havent discussed that yet so my prof might question it..i made a program of how i understand it... but it seems it does'nt work at all..here it goes..

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{char fsentence[30],fword[20],flongest[]="",fflongest[20],j[10],r='\0';
int abc,def,ghi=0,lng=0;
printf("Input:  ");
scanf("%s",&fsentence);
j[0]=fsentence[0];;
for(def=0;def<=strlen(fsentence);def++)
{if(isspace(fsentence[def])!=0)
{if(ghi>=lng)
{lng=ghi;
ghi=0;}
}
else
ghi++;}ghi=0;
for(def=0;def<=strlen(fsentence);def++)
{if(isspace(fsentence[def])!=0)
{if(ghi==lng)
{strncpy(fflongest,fsentence,def);
puts("fflongest");
strnset(fsentence,r,def);
ghi=0;
}else
ghi=0;strnset(fsentence,r,def);
}else
ghi++;
}
getch();}

Hi its as ur prof said usage of strtok fun eases it.STRTOK when used for the first time marks the first char in the first str as NULL and so next time it can be called as strtok(NULL,"<delimiter>"); following function can be easily written as:

#include<stdio.h>
#include<string.h>
int main()
{
int maxs=0;
char *s,*p,*max;
printf("Enter input string : ");
scanf("%[^\n]",s);
p=strtok(s," ");
if(strlen(p)>maxs)
{
	maxs=strlen(p);
	max=p;
}
p=strtok(NULL," ");
while(p)
{
	if(strlen(p)>maxs)
	{
		maxs=strlen(p);
		max=p;
	}
p=strtok(NULL," ");
}
printf("\n\nMax Length of str:%d  Max String:%s",maxs,max);
return 0;
}

>following function can be easily written as:
No!, it can not be written as easily. char *s; is a pointer to a char and you can not write to memory that it hasn't been allocated to it.
Thus, scanf("%[^\n]",s); is wrong, and so is p=strtok(s," ") ; since strtok need to modify s.
I stopped looking at your code beyond that.

The use of code tags to correctly post snippets of code is not optional. Please, learn how to use them and do it.
Click me for an example.

i need to output the longest word not the largest size...

the program should output all the words with the longest word...
example...
Input: The quick brown fox jumps over the lazy dog.
Output: quick, brown, jumps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.