Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include <stdio.h>

int main()
{
	char str[100],sav[100],tmp[100];
	int i,count=0,k=0,len,flag,a[10];

	printf("Enter string:");
	gets(str);

	printf("\nYou entered:");
	puts(str);
	printf("\n");
	len=strlen(str);


	while(1)
	{
		for(i=0;str[i]!='\0';i++)
		{
			count++;
			sav[i]=str[i];

			if(str[i] == ' ' || str[i] !='\0')
			{
				if(k<count)>
				{
					printf("No. of words is:%d\n",count);
					k=count;
					printf("Words separated by spaces:");
					puts(sav);
					printf("\n");
				}
				count = 0;
			}
		}
	}

	return 0;

}
Posted
Updated 11-Oct-13 12:28pm
v4
Comments
Valery Possoz 11-Oct-13 18:04pm    
And... what do you want? sorry but I don't get it.
Member 10331161 11-Oct-13 18:10pm    
suppose ...the input is
I live in the USA....
output should be:
live......
because live has 4 letters!
CHill60 11-Oct-13 18:12pm    
What Valery means is what is your problem? You've stated what your homework says and posted some code, but haven't said what the problem is.
Member 10331161 11-Oct-13 18:16pm    
im not able to get it......my code doesnot work...i need some modification for this code
Member 10331161 11-Oct-13 18:17pm    
there is some problem in while loop....not able to solve it....stuck at this problem.

1 solution

This is homework, and we don't do homework! :laugh:

But your code doesn't work, because you haven't told it to do what you want it to - it doesn't look at the string and work out any word lengths, which you have to do before you can work out which word is longest.

The easiest way to do this is to create three functions
1) get the user string.
2) find each word.
3) find the longest word.

That way, you code becomes a long simpler, and more obvious to both read, and write.

I'm not sure which way to suggest you do this, but... Do you know how to set up arrays? And what pointers are?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900