Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Question
Read a string with words separated by space and return its length and count of consonants.
Sample Input
write a program
Sample Output
158
Solution
Sure, here is a Python solution for your problem:
def count_consonants_and_length(input_string):
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
consonant_count = 0
for char in input_string:
if char in consonants:
consonant_count += 1
return len(input_string), consonant_count
input_string = "write a program"
length, consonant_count = count_consonants_and_length(input_string)
print(length, consonant_count)
This program defines a function count_consonants_and_length
that takes a string as input. It first defines a string consonants
that contains all the consonants in the English alphabet, both in lower and upper case. It then initializes a counter consonant_count
to 0. It then iterates over each character in the input string, and if the character is in the consonants
string, it increments the consonant_count
. Finally, it returns the length of the input string and the consonant_count
.
The program then tests this function with the string "write a program", and prints the length of the string and the count of consonants.
Similar Questions
IPS5-Pointer5Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Write a program to accept a string as input and print the number of consonants in the string.Input:StringOutput:IntegerConstraints1<=length of string <=1000
Write a program to count number of words in a given string whose length is greater than 3.For example:Input Resultpython is easier than C3
Python program that accepts a string and calculate the number of digits and letters and space
Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.