Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   write a prog to count the no word,space,sentence,vowels,and constants (http://www.go4expert.com/forums/write-prog-count-t27119/)

vikas k verma 9Nov2011 11:15

write a prog to count the no word,space,sentence,vowels,and constants
 
Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int i,len=0,word=1,sant=0,space=0,vow=0,cons=0,line=1;
clrscr();
printf("plz enter the string\n");
gets(a);
/*len=strlen(a);*/
/* OR */
for(i=0;a[i]!='\0';i++)
{
  len++;
}
for(i=0;i<=len;i++)
{
 if(a[i]==' ')
 {
  space++;
 }
 if(a[i]==' '&&(a[i]>='a'||a[i]<='z'))
 {
  word++;
 }
 else if(a[i]=='.')
 {
  sant++;
 }
 else if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
 {
  vow++;
 }
 else if(a[i]=='b'||a[i]=='c'||a[i]=='d'||a[i]=='f'||a[i]=='g'||a[i]=='h'||a[i]=='j'||a[i]=='k'||a[i]=='l'||a[i]=='m'||a[i]=='n'||a[i]=='p'||a[i]=='q'||a[i]=='r'||a[i]=='s'||a[i]=='t'||a[i]=='v'||a[i]=='w'||a[i]=='x'||a[i]=='y'||a[i]=='z'||a[i]=='B'||a[i]=='C'||a[i]=='D'||a[i]=='F'||a[i]=='G'||a[i]=='H'||a[i]=='J'||a[i]=='K'||a[i]=='L'||a[i]=='M'||a[i]=='N'||a[i]=='P'||a[i]=='Q'||a[i]=='R'||a[i]=='S'||a[i]=='T'||a[i]=='V'||a[i]=='W'||a[i]=='X'||a[i]=='Y'||a[i]=='Z')
 {
 cons++;
 }
 else if(a[i]=='\n')
 {
 line++;
 }
}
printf(" length=%d\n words=%d\n space=%d\n santence=%d\n vowel=%d\n consonent=%d\n",len,word,space,sant,vow,cons);
printf(" line=%d",line);
getch();
}


xpi0t0s 10Nov2011 17:18

Re: write a prog to count the no word,space,sentence,vowels,and constants
 
Interesting, could you explain what you think this will do?
Code:

if(a[i]==' '&&(a[i]>='a'||a[i]<='z'))

poornaMoksha 16Nov2011 17:22

Re: write a prog to count the no word,space,sentence,vowels,and constants
 
Quote:

Originally Posted by vikas k verma (Post 88994)
Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int i,len=0,word=1,sant=0,space=0,vow=0,cons=0,line=1;
clrscr();
printf("plz enter the string\n");
gets(a);
/*len=strlen(a);*/
/* OR */
for(i=0;a[i]!='\0';i++)
{
  len++;
}
for(i=0;i<=len;i++)
{
 if(a[i]==' ')
 {
  space++;
 }
 if(a[i]==' '&&(a[i]>='a'||a[i]<='z'))
 {
  word++;
 }
 else if(a[i]=='.')
 {
  sant++;
 }
 else if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
 {
  vow++;
 }
 else if(a[i]=='b'||a[i]=='c'||a[i]=='d'||a[i]=='f'||a[i]=='g'||a[i]=='h'||a[i]=='j'||a[i]=='k'||a[i]=='l'||a[i]=='m'||a[i]=='n'||a[i]=='p'||a[i]=='q'||a[i]=='r'||a[i]=='s'||a[i]=='t'||a[i]=='v'||a[i]=='w'||a[i]=='x'||a[i]=='y'||a[i]=='z'||a[i]=='B'||a[i]=='C'||a[i]=='D'||a[i]=='F'||a[i]=='G'||a[i]=='H'||a[i]=='J'||a[i]=='K'||a[i]=='L'||a[i]=='M'||a[i]=='N'||a[i]=='P'||a[i]=='Q'||a[i]=='R'||a[i]=='S'||a[i]=='T'||a[i]=='V'||a[i]=='W'||a[i]=='X'||a[i]=='Y'||a[i]=='Z')
 {
 cons++;
 }
 else if(a[i]=='\n')
 {
 line++;
 }
}
printf(" length=%d\n words=%d\n space=%d\n santence=%d\n vowel=%d\n consonent=%d\n",len,word,space,sant,vow,cons);
printf(" line=%d",line);
getch();
}


Please dont use 'gets()' in your code. It is a handy tool for hackers to intrude into your programs.

Spare some time and read :
http://cboard.cprogramming.com/c-pro...dangerous.html


Also, as xpi0t0s said, the condition :

Code:

if(a[i]==' '&&(a[i]>='a'||a[i]<='z'))
does not make any sense. '&&' means that both conditions should be true to enter the 'if' block. But how is this possible for a[i] to be equal to space ' ' and some alphabet (a-z) at the same time?


All times are GMT +5.5. The time now is 02:17.