Practice Program







  • Program to Count the vowels in a String.
  • Solution:
    #include<stdio.h>
    void main()
    {
        int i,count=0;
        char s[20];
    
        printf("Enter the string: ");
        gets(s);
    
        printf("String is:");
        puts(s);
    
        for(i=0;s[i] != '\0';i++)
        {
             switch(s[i])//switching the character.
            { //if character is a,e,i,o,u
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            count++;
        }
    
    
    }
    printf("There are %d vowels in ",count);
        puts(s);
    
    }
    

    Output1
    Enter the string: Computer

    String is : Computer
    There are 3 vowels in Computer

    Output2
    Enter the string: Programmer

    String is : Programmer
    There are 3 vowels in Programmer

    Output3
    Enter the string: hello

    String is : hello
    There are 2 vowels in hello









    defining a function 'count_vowels' which will return number of vowels in String

    int count_vowels(char s[])
    {
        int i,count=0;
    
        for(i=0;s[i] != '\0';i++)
        {
            switch(s[i])
            {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            count++;
    
            }
    
    
        }
        return count;
    }
    

    Using above define function 'count_vowels()':
    void main()
    {
        int result;
    
    
        result = count_vowels("Computer");
    
        printf("There are %d vowels",result);
    }
    

    Output1
    There are 3 vowels

    Using above define function 'count_vowels()':
    void main()
    {
        int result;
    
    
        result = count_vowels("Programmer");
    
        printf("There are %d vowels",result);
    }
    

    Output2
    There are 3 vowels

    Using above define function 'count_vowels()'
    void main()
    {
        int result;
    
    
        result = count_vowels("hello");
    
        printf("There are %d vowels",result);
    }
    

    Output3
    There are 2 vowels