Question

Vowels and consonants Write a program that reads a word and prints the number of vowels...

Vowels and consonants
Write a program that reads a word and prints the number of vowels and consonants in the
word. For this exercise assume that ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and ‘y’ are vowels. For
example, if the user enters the input “Harry”, the program should print “The word
contains 2 vowels and 3 consonants”.

Homework Answers

Answer #1

In c++

#include<bits/stdc++.h>

using namespace std;

int main()
{
        //declaring the char array
        char arr[100];
        cout<<"Enter the word : ";
        //taking the word input
        cin>>arr;
        int cnst,vow,i;
        cnst=vow=i=0;
        //traversing the array till it founds the end end alphabet of the word
        while(arr[i]!='\0')
        {
                //checking for vowels
                if(arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u' || arr[i]=='y')
                {
                        vow++;
                }
                //checking for capital letter vowels
                else if(arr[i]=='A' || arr[i]=='E' || arr[i]=='I' || arr[i]=='O' || arr[i]=='U' || arr[i]=='Y')
                {
                        vow++;
                }
                //else counting consonants
                else
                {
                        cnst++;
                }
                i++;
        }
        
        cout<<"The word contains "<<vow<<" vowels"<<" and "<<cnst<<" consonants.";
        
        return 0;
}

In Java

import java.util.*;

public class Main
{
        public static void main(String[] args)
        {
            //initializing the Scanner
                Scanner sc=new Scanner(System.in);
                String word;
                int n,i,vow,cnst;
                char check;
                vow=cnst=0;
                System.out.print("Enter the word : ");
                //taking the input
                word=sc.next();
                //calculating the word length
                n=word.length();
                for(i=0;i<n;i++)
                {
                    check=word.charAt(i);
                    //checking for vowels
                if(check=='a' || check=='e' || check=='i' || check=='o' || check=='u' || check=='y')
                {
                        vow++;
                }
                //checking for capital letter vowels
                else if(check=='A' || check=='E' || check=='I' || check=='O' || check=='U' || check=='Y')
                {
                        vow++;
                }
                //else counting consonants
                else
                {
                        cnst++;
                }
                }
                System.out.println("The word contains "+vow+" vowels and "+cnst+" consonants.");

        }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that reads a word and then prints the word in reverse separated by...
Write a program that reads a word and then prints the word in reverse separated by a comma. 3 pts Note: No comma at the end of the reversed word. Enter word: Harry The reversed word is: y,r,r,a,H Python keep it simple
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Write a program that asks the user to enter an odd number and prints out a...
Write a program that asks the user to enter an odd number and prints out a triangle pattern. For example, if the user enters 5, the output is note: There is one space between the numbers in each line 1 3 5 1 3 1 If the user enters 9 the output is: 1 3 5 7 9 1 3 5 7 1 3 5 1 3 1 program language: C++
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
4. The Hawaiian alphabet has twelve letters: five vowels (a, e, i, o, and u) and...
4. The Hawaiian alphabet has twelve letters: five vowels (a, e, i, o, and u) and seven consonants (h, k, l, m, n, p, and w). For the purpose of this exercise we will define an n–letter “word” as an ordered collection of n of these twelve letters with repeats allowed. Obviously, most such “words” will be nonsense words.   e) What is the probability a randomly selected four–letter “word” contains exactly one consonant?
The Hawaiian alphabet has twelve letters: five vowels (a, e, i, o, and u) and seven...
The Hawaiian alphabet has twelve letters: five vowels (a, e, i, o, and u) and seven consonants (h, k, l, m, n, p, and w). For the purpose of this exercise we will define an n–letter “word” as an ordered collection of n of these twelve letters with repeats allowed. Obviously, most such “words” will be nonsense words. What is the probability a randomly selected four–letter “word” contains exactly one consonant?
C++ INSTRUCTIONS Problem Specification : Write a program that reads a group of numbers from the...
C++ INSTRUCTIONS Problem Specification : Write a program that reads a group of numbers from the user and places them in an array of type float. Once the numbers are stored in the array, the program should average and print the result. Use pointer notation wherever possible. Program Output (with Input Shown in Bold): Enter number: 2 Enter another (y/n)? y Enter number: 4 Enter another (y/n)? y Enter number: 6 Enter another (y/n)? y Enter number: 8 Enter another...