longest sentence, need help

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int x=0,y=0,max=0;
char st[100];
char at[]="this is the end";
printf ("\n give me a setence:");
fflush(stdin);
gets(st);
for (int i=0;i<100;i++)
{
if ((st[i]!=' ')&&(st[i+1]==' '))
x++;
if (st[i]=='\0')
break;
}
x++;
max=x;
do
{
printf ("\n give me a sentence:");
fflush (stdin);
gets(st);
if (st[0]!=' ')
y++;
for (int i=0;i<100;i++)
{
if ((st[i]!=' ')&&(st[i+1]==' '))
y++;
if (st[i]=='\0')
break;
}
if (y>x)
max=y;
else
max=x;
y=0;
}while (strcmp(st, at)!=0);
printf ("\n the number of words is %d",max);
getch();
}

Last edited on
Is the lohgest sentence the sentence that has the greatest number of words within it or that has the longest length?
the sentence that has the greatest number of words
Last edited on
i need a program that asks the user to enter sentences until he enters "this is the end". and then shows the sentence entered that has the greatest number of words
You can use standard function strtok to split a sentence into words.
Or you can write two macros or inline functions something as SkipSpaces and ExtractWord to count the number of words in a sentence.

EDIT: For example

#define SkipSpaces( p ) while ( isblank( *p ) ) ++p
#define ExtractWord( p ) while ( *p && !isblank( *p ) ) ++p
#define IsEmpty( p ) ( *p == '\0' )

Then to determine the number of words you can use the following loop

1
2
3
4
5
6
7
8
9
10
11
12
int count = 0;
char *p   = st;

while ( !IsEmpty( p ) )
{
   SkipSpaces( p );
   if ( !IsEmpty( p ) )
   {
      count++;
      ExtractWord( p );
   }
}

Last edited on
Here is a tested code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>

/*	MS VC++ 2010 does not support C standard function isblank.
	So I changed it to explicit using space and tab symbols
*/
#define	SkipSpaces( p ) while ( *p == ' ' || *p == '\t' ) ++p
#define	ExtractWord( p ) while ( *p && *p != ' ' && *p != '\t' ) ++p
#define	IsEmpty( p ) ( *p == '\0' )


int main( void )
{
	char s[] = "This is some sentence.";

	size_t count = 0;
	char *p = s;

	while ( !IsEmpty( p ) )
	{
		SkipSpaces( p );
		if ( !IsEmpty( p ) )
		{
			count++;
			ExtractWord( p );
		}
	}

	printf( "There are %u words in the sentence", count );
}



The ouput is

There are 4 words in the sentence



And this code was tested at www.ideon.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define	SkipSpaces( p ) while ( isblank( *p ) ) ++p
#define	ExtractWord( p ) while ( *p && !isblank( *p ) ) ++p
#define	IsEmpty( p ) ( *p == '\0' )


int main( void )
{
	char s[] = "This is some sentence.";

	size_t count = 0;
	char *p = s;

	while ( !IsEmpty( p ) )
	{
		SkipSpaces( p );
		if ( !IsEmpty( p ) )
		{
			count++;
			ExtractWord( p );
		}
	}

	printf( "There are %u words in the sentence", count );
    
	return 0;
}



Of course it would be better to substitute these two statements

1
2
3
	char *p = s;

	while ( !IsEmpty( p ) )


for this one

for ( char *p = s; !IsEmpty( p ); )
Last edited on
Topic archived. No new replies allowed.