Answer to Question #215976 in C++ for Hemambar

Question #215976
given an input string Ayodhya your C program to count the number of vowels and consonants and print the frequency of occurrence of each vowel and consonant delete the vowels in the same input string and display the updated string
Sample input:
Enter the input string: Ayodhya
Sample output:
No of vowels:3
Frequency of occurrence:A-2;o-1
No of consonants:4
Frequency of occurrence:y-2;d-1;h-1
1
Expert's answer
2021-07-11T14:39:05-0400
#include <iostream>

bool isVowel(char c)
{
    switch(toupper(c))
    {
        case 'A': case 'E': case 'I': case 'O': case 'U' : return true;
        default: return false;
    }
}

int main()
{
    const char* source = "Ayodhya";
    const char* tmp = source;

    char uppercaseLetters[26] = {};
    char lowercaseLetters[26] = {};
    int vowels = 0;
    int consonants = 0;
    bool flag = false;

    while(*tmp)
    {
        char current = tmp[0];
        
        if(isalpha(current))
        {
            if(isVowel(toupper(current)))
            {
                ++vowels;
            }
            else
            {
                ++consonants;
            }
            
            if(isupper(current))
            {
                ++uppercaseLetters[current - 'A'];
            }
            else
            {
                ++lowercaseLetters[current - 'a'];
            }
        }
        
        ++tmp;
    }

    std::cout << "No of vowels: " << vowels << "\n";
    std::cout << "Frequency of occurrence: ";
    
    for(int i = 0; i < sizeof(uppercaseLetters)/sizeof(uppercaseLetters[0]); ++i)
    {
        char current = 'A' + i;
        
        if(uppercaseLetters[i] + lowercaseLetters[i] > 0)
        {
            if(isVowel(current))
            {
                if(!flag)
                {
                    flag = true;
                }
                else
                {
                    std::cout << ";";
                }
                
                if(uppercaseLetters[i] > 0)
                {
                    std::cout << (char)toupper(current) << "-" << (uppercaseLetters[i] + lowercaseLetters[i]);
                }
                else
                {
                    std::cout << (char)tolower(current) << "-" << (uppercaseLetters[i] + lowercaseLetters[i]);
                }
            }
        }
    }

    std::cout << "\n";
    std::cout << "No of consonants: " << consonants << "\n";
    std::cout << "Frequency of occurrence: ";
    flag = false;

    for(int i = 0; i < sizeof(uppercaseLetters)/sizeof(uppercaseLetters[0]); ++i)
    {
        char current = 'A' + i;
        
        if(uppercaseLetters[i] + lowercaseLetters[i] > 0)
        {
            if(!isVowel(current))
            {
                if(!flag)
                {
                    flag = true;
                }
                else
                {
                    std::cout << ";";
                }
                
                if(uppercaseLetters[i] > 0)
                {
                    std::cout << (char)toupper(current) << "-" << (uppercaseLetters[i] + lowercaseLetters[i]);
                }
                else
                {
                    std::cout << (char)tolower(current) << "-" << (uppercaseLetters[i] + lowercaseLetters[i]);
                }
            }
        }
    }

    std::cout << "\n";
    std::cout << "String without vowels: ";
    tmp = source;
    while(*tmp)
    {
        char current = tmp[0];
        
        if(!(isalpha(current) && isVowel(toupper(current))))
        {
            std::cout << current;
        }
        
        ++tmp;
    }

    std::cout << "\n";
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS