In C++,
Write two programs to count the number of consonants in a string. One will use an iterative method and one will use a recursive method.
ANSWER:-
(1) iterative method :-
#include <iostream>
using namespace std;
int totalConsonants(string str)
{
int count = 0;
char ch;
for (int i = 0; i < str.length(); i++)
{
ch=str[i];
ch = toupper(ch);
if(!(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') && ch >= 65 && ch <= 90)
++count;
}
return count;
}
int main()
{
string str;
cout<<"Enter astring : abcdef\n";
cin>>str;
cout <<"the total consonants :
"<<totalConsonants(str);
return 0;
}
(2) recursive method :-
#include <iostream>
using namespace std;
bool isConsonant(char ch)
{
ch = toupper(ch);
return !(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') && ch >= 65 && ch <= 90;
}
// to count total number of consonants from
// 0 to n-1
int totalConsonants(string str, int n)
{
if (n == 1)
return isConsonant(str[0]);
return totalConsonants(str, n - 1) +
isConsonant(str[n-1]);
}
int main()
{
string str;
cout<<"Enter astring : abcdef\n";
cin>>str;
cout <<"the total consonants : "<<totalConsonants(str,
str.length());
return 0;
}
// OUTPUT

// If any doubt please comment
In C++, Write two programs to count the number of consonants in a string. One will...
I need this in Visual Studio C++
Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write...
(Count vowels and consonants) Assume letters A, E, I, O, and U as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string. using python
c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.
‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...
Appreciate a step by step explanation to this question.
Write two separate programs in python that generate the n-th Fibonacci number. The programs should both take n as command line input. The first algorithm one should implement the recursive algorithm, and the second one should implement the iterative algorithm. Measure the time it takes for each of these versions (use for example, the time it module in python) to calculate the n-th Fibonacci number for the following values of n:...
(Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O and U are the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop: Read one line from the input file and store it in a string; Count the number of vowels and consonants (using either while-loop or for-loop) in the file string. The while-loop...
Write a Pelles C program that takes a char array (string) and counts the number of consonants and vowels using a pointer
In C++, you’ll be generating a program that performs several
different functions in two different manners, iterative, and
recursive. For the recursive portion of the program, you must use a
function to pass data along. The program must be capable of doing
the following, and without using the pre-existing mathematical
functions for factorial, power, and so on.
Given one number, calculate the factorial of that given number.
i.e. 7! = 5,040
Given one number, and one power, calculate the number...
Write a recursive method that counts the number of vowels in a string. It must return the number of vowels. Use appropriate parameters as needed. Hint: There's an ArrayList method called contains that might make checking whether a character is a vowel easier.
Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string (that can include upper and lowercase characters) and returns the number of consonants in that string. A consonant is defined as any letter in the English alphabet other than the vowels a, e, i, o, u. For example, if you call the function as follows: count_consonants('correct horse battery staple') the function should return 20.