3 Replies - 14605 Views - Last Post: 25 April 2008 - 02:54 AM Rate Topic: -----

#1 umairqureshi_6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 08

Word Count in C

Posted 23 April 2008 - 08:41 AM

how to count words in a sentence
i have made a program by using spaces
that it counts the no of spaces

if the no of spaces are 3
then words should b 4

but if user gives double space
then this program fails
i cannot use functions

here's the coding


/*to count character,vowels,the,spaces*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a, spaces=0,chr=0,vowels=0,ok=0,the=0,words=0;
char c;
clrscr();
do
{
c=getche();
a=c;

if(c==' ')
spaces++;

if((c=='a')||(c=='A')||(c=='e')||(c=='E')||(c=='i')||(c=='I')||(c=='O')||
(c=='o')||(c=='u')||(c=='U'))
vowels++;

if (c==' ')
words++;

if((c=='t')||(c=='h')||(c=='e')||(c=='T')||(c=='H')||(c=='E'))
{
	if((c=='t')||(c=='T'))
	{
	ok=1;
	}
	if((ok==1)&&(c=='h')||(c=='H'))
	{
	ok=2;
	}
	if((ok==2)&&(c=='e')||(c=='E'))
	{
	ok=3;
	the++;
	}
}
else
{
ok=0;
}
chr++;
}
while(a!=13);

printf("\n\n Total characters==>%d \n\n Spaces==>%d \n\n Vowels==>%d \n\n The==> %d \n\n words==> %d",chr,spaces,vowels,the,words+1);
getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Word Count in C

#2 Santhanalakhsmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-April 08

Re: Word Count in C

Posted 24 April 2008 - 03:19 AM

View Postumairqureshi_6, on 23 Apr, 2008 - 08:41 AM, said:

how to count words in a sentence
i have made a program by using spaces
that it counts the no of spaces

if the no of spaces are 3
then words should b 4

but if user gives double space
then this program fails
i cannot use functions

here's the coding


/*to count character,vowels,the,spaces*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a, spaces=0,chr=0,vowels=0,ok=0,the=0,words=0;
char c;
clrscr();
do
{
c=getche();
a=c;

if(c==' ')
spaces++;

if((c=='a')||(c=='A')||(c=='e')||(c=='E')||(c=='i')||(c=='I')||(c=='O')||
(c=='o')||(c=='u')||(c=='U'))
vowels++;

if (c==' ')
words++;

if((c=='t')||(c=='h')||(c=='e')||(c=='T')||(c=='H')||(c=='E'))
{
	if((c=='t')||(c=='T'))
	{
	ok=1;
	}
	if((ok==1)&&(c=='h')||(c=='H'))
	{
	ok=2;
	}
	if((ok==2)&&(c=='e')||(c=='E'))
	{
	ok=3;
	the++;
	}
}
else
{
ok=0;
}
chr++;
}
while(a!=13);

printf("\n\n Total characters==>%d \n\n Spaces==>%d \n\n Vowels==>%d \n\n The==> %d \n\n words==> %d",chr,spaces,vowels,the,words+1);
getch();
}




word count in c:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,c=1;
char line[50];
clrscr();
printf("Enter any line\n");
gets(line);
for(i=0;line[i]!='\0';i++)
{
if(line[i]==' ')
c++;
}
printf("No.of word in a line=%d",c);
getch();
}
Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Word Count in C

Posted 24 April 2008 - 03:47 AM

Quote

word count in c:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,c=1;
char line[50];
clrscr();
printf("Enter any line\n");
gets(line);
for(i=0;line[i]!='\0';i++)
{
if(line[i]==' ')
c++;
}
printf("No.of word in a line=%d",c);
getch();
}


That's OK, but they were asking how to ignore double spaces. All you need to do for this is add a tiny little bit in the if(line[i] == ' ') part. Here:
#include<stdio.h>
#include<conio.h>

int main()
{
	int i,c=1;
	char line[50];
	printf("Enter any line\n");
	gets(line);
	for(i=0;line[i]!='\0';i++) 
	{
	if(line[i]==' ' && line[i+1] != ' ') // check the next character with [i+1]
	c++;
	}
	printf("No.of word in a line=%d",c);
	getch();
}



Hope this helps :)
Was This Post Helpful? 1

#4 umairqureshi_6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 08

Re: Word Count in C

Posted 25 April 2008 - 02:54 AM

thx for ur support:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1