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

Discussion in 'C' started by vikas k verma, Nov 9, 2011.

  1. vikas k verma

    vikas k verma New Member

    Joined:
    Nov 9, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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();
    }
     
    Last edited by a moderator: Nov 9, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Interesting, could you explain what you think this will do?
    Code:
    if(a[i]==' '&&(a[i]>='a'||a[i]<='z'))
    
     
  3. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    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-programming/25338-why-gets-function-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 to be equal to space ' ' and some alphabet (a-z) at the same time?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice