Thread: Count the number of vowels, consonants, digits etc.

  1. #1
    Registered User
    Join Date
    Mar 2005

    Smile Count the number of vowels, consonants, digits etc.

    Hi! All
    Here I am trying to write a programme, which will accept a line of text and count the vowels, consonants, digits, whitespaces & others like (.,;,).
    The code i am using is :
    void scan_line(char *line, int *pv, int *pc, int *pd, int *pw, int *po);
    void main()
      char *line;
      int vowels=0;
      int consonants=0;
      int digits=0;
      int whitespace=0;
      int others=0;
      printf("Enter a line of text below : \n");
      printf("\n No. of vowels: %d",vowels);
      printf("\n No. of consonants: %d",consonants);
      printf("\n No. of digits: %d",digits);
      printf("\n No. of whitespace: %d",whitespace);
      printf("\n No. of other characters: %d",others);
    void scan_line(char *line, int *pv, int *pc, int *pd, int *pw, int *po)
      char c;
      int count=0;
         else if(c>='A'&& c<='Z')
         else if(c>='0'&& c<='9')
         else if(c==' '|| c=='\t')
       return ;
    But it is not returning the desired result. 
    Please help Anil.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Firstly don't have a void return type for main.

    What are you trying to do here? line is a character pointer that points to a random location - you need to allocate memory to it using malloc() or create a static sized array for writing.

    Another problem you will encounter is that you are checking for '\n' using scanf(). scanf() won't actually put the newline character into the string so you are going to go out of bounds in the array checking part. If you insist on using scanf() then check for a nul-byte character '\0' instead of '\n'. A better way to read user input would be to use fgets().
    Last edited by 0rion; 05-07-2005 at 01:26 AM.
    The cost of software maintenance increases with the square of the programmer's creativity.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe

    There's no need to include this.
    1. If you're running in an IDE, clicking on your executable starts with a blank screen anyway, so the first line of clrscr() is well, pointess.
    2. The getch(); at the end is also non-portable. Check the FAQ for alternatives.

    > void main()
    main returns an int

    > char *line;
    Where is this pointing? Nowhere is the answer.
    Input to your program just trashes random bits of memory until the code mysteriously crashes.

    char line[100];

    > scanf("%s",line);
    fgets( line, sizeof line, stdin );
    Would be better.

    Not all input strings are guaranteed to have a newline.
    In the first instance, you should check for a \0
    while ( line[count] != '\0' && line[count] != '\n' ) {
      c = toupper( line[count] );
    > ++*pv;
    You might find that
    work better.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Thanx all I have got ur points and implemented ur techniques. Now I have got the appropriate answer too.
    Thanx again to all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 09:37 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  4. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 10:39 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM