Thread: Counting vowels

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    Counting vowels

    I have to write a program that takes a series of alphabets and displays the repeating vowels and their corresponding occurrence. The input can be either lower case or upper case. For help, i can convert upper case to lower case and vice versa without affecting other characters.

    Sample input: AsdatEhjTeEUuklIti
    Sample output: Repeating vowels are
    a 2
    e 3
    i 2
    u 2

    I came up with this code:

    Code:
    #include <stdio.h>
    
    
    char tolower(char p);
    char toupper(char p);
    
    
    int main()
    {
    
    
        int z,p=1,a=0,i=0,o=0,u=0,e=0, f;
    
    
        char alpha[p];
        char conv[f];
    
    
        printf("\n Enter characters : \t");
    
    
        while(scanf("%c",&alpha[p])!=EOF)
        {
            if(alpha[p]='A'||'E'||'I'||'O'||'U')
            {
                conv[f]=tolower(alpha[p]);
            }
    
    
            switch (conv[f])
            {
    
    
            case 'a' :
            {
                a++;
                break;
            }
            case 'e' :
            {
                e++;
                break;
            }
            case 'i' :
            {
                i++;
                break;
            }
            case 'o' :
            {
                o++;
                break;
            }
            case 'u' :
            {
                u++;
                break;
            }
    
    
            default :
            {
                break;
            }
            }
            f++;
    
    
        }
    
        printf("\n a=\t %d",a);
        printf("\n e=\t %d",e);
        printf("\n i=\t %d",i);
        printf("\n o=\t %d",o);
        printf("\n u=\t %d",u);
    
        return 0;
    }
    
    
    char toupper(char p)
    {
        int x;
        x = (int)p - 32;
    
    
        return x;
    }
    
    
    char tolower(char p)
    {
        int x;
        x = (int)p + 32;
    
    
        return x;
    }


    Any idea how to proceed to a right solution? Thanks
    Last edited by skg29; 04-28-2012 at 11:40 PM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Your code has following errors:

    wrong declaration of array inside main
    Code:
        char alpha[p];
        char conv[f];
    and..
    The if condition
    Code:
    if(alpha[p]='A'||'E'||'I'||'O'||'U')
    I did some modification in your code and I think its working now.. check this out
    Code:
    #include <stdio.h>
    
    
    char tolower(char p);
    char toupper(char p);
    
    
    int main()
    {
    
    
        int p=0,a=0,i=0,o=0,u=0,e=0, f;
    
        char alpha[20];
    
        printf("\n Enter characters : \t");
     
       scanf("%s",&alpha);
        
        while( alpha[p]!=EOF)
         {
            if(alpha[p]=='A'||alpha[p]=='E'||alpha[p]=='I'||alpha[p]=='O'||alpha[p]=='U')
            {
                alpha[p]=tolower(alpha[p]);
            }
    
    
            switch (alpha[p])
            {
    
    
            case 'a' :
            {
                a++;
                break;
            }
            case 'e' :
            {
                e++;
                break;
            }
            case 'i' :
            {
                i++;
                break;
            }
            case 'o' :
            {
                o++;
                break;
            }
            case 'u' :
            {
                u++;
                break;
            }
            }  //End of Switch
            p++;
    
    
        }  //End of While
    
        printf("\n a=\t %d",a);
        printf("\n e=\t %d",e);
        printf("\n i=\t %d",i);
        printf("\n o=\t %d",o);
        printf("\n u=\t %d",u);
    
        return 0;
    }  //End of Main
    
    
    char toupper(char p)
    {
        int x;
        x = (int)p - 32;
    
    
        return x;
    }
    
    
    char tolower(char p)
    {
        int x;
        x = (int)p + 32;
    
    
        return x;
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Yes it does work. I did not know about that if. Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("%s",&alpha);
    You should drop the &
    alpha is an array of char, so there is no need for an address operator here.
    If alpha were an actual char* (pointing to some memory), then using & would make it very wrong.

    > while( alpha[p]!=EOF)
    EOF is returned by FILE* stream functions, it is never stored in a string. \0 marks the end of strings.

    > case 'a' :
    Saying
    case 'a' : case 'A':
    would save all the toupper stuff.

    > x = (int)p - 32;
    This assumes an ASCII character set (look up EBCDIC)
    Besides, portable versions are in ctype.h
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2012, 03:41 PM
  2. Counting vowels in a string - invalid identifier
    By hencherz in forum C++ Programming
    Replies: 11
    Last Post: 02-25-2012, 05:38 AM
  3. Need help with counting vowels prog
    By truetrini20 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2010, 07:44 AM
  4. Counting Vowels within a string.
    By patso in forum C Programming
    Replies: 12
    Last Post: 04-09-2008, 04:21 PM
  5. counting vowels
    By trippedwire in forum C++ Programming
    Replies: 10
    Last Post: 10-01-2004, 11:58 PM