#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();
}
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 );
}
}
#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 );
}