how to write a function in python 3 using for loop that counts how many times a consonant (either lowercase or uppercase) occurs in a string. It takes a string as input and returns an integer which is the number of consonant in the string. Also the function should not use brute force solution meaning comparing one consonant at a time.

Question
Asked Mar 3, 2019
50 views

how to write a function in python 3 using for loop that counts how many times a consonant (either lowercase or uppercase) occurs in a string. It takes a string as input and returns an integer which is the number of consonant in the string. Also the function should not use brute force solution meaning comparing one consonant at a time. 

check_circle

Expert Answer

Step 1

Note: The given program is compiled and executed in "repl.it" online compiler.

Step 2

Screenshot of code:

fullscreen
Step 3

Editable code:

 

#Defintion of function count_const() to count number of consonants
def count_const(user_str):

#Create a counter variable"count" and assign 0 to it
count =0

#use loop to check each character in the user_str
for var in user_str:

#Check if current character is the alphabet
if var.isalpha():

#Check if character is not the vowel
if var not in "aeiouAEIOU":

#Increment the counter "count" by 1
count += 1

#Return the cou...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Python

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Programming in C

A: The program would have the following steps:Declare the arrays to accept the user input and the array...

question_answer

Q: Design a Turing machine that computes the function f (x) = x − 2 if x > 2   = 0 i...

A: Click to see the answer

question_answer

Q: Assume that an array of integers named a that contains exactly five elements has been declared and i...

A: Didn’t mention which language to do the problem. So, did the problem in Java.Given:Let assume an arr...

question_answer

Q: write a program to enter two sides a and b of a right triangle from keyboard calculate its area and ...

A: Given below is the C++ program to enter the two sides of a right triangle and then calculate its are...

question_answer

Q: Use Java 5-1) (Count positive and negative numbers and compute the average of numbers) Write a progr...

A: Note: The below program has been complied and executed in NetBeans IDE 8.2

question_answer

Q: What is the best way to pass 2D arrays to a function? (C++) For my program, I have a 2D array (size:...

A: There are two ways to pass 2-D array to a function either by sending the array name in the function ...

question_answer

Q: How to implement a pluggable Adapter design pattern in Java?

A: Let’s say we have a client class called Client.java which calls method1( ) of an existing library cl...

question_answer

Q: Part 1: Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue libr...

A: Import the given class in another file.Create the function postfix() which takes a string as its arg...

question_answer

Q: Find an nfa that accepts the complement from L (ab∗aa + bba∗ab)?

A: Solution:The following graph indicates the NFA (Non-deterministic finite automata) is given below:M ...