IPS5-Pointer5Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Question
IPS5-Pointer5Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Solution
Sure, here is a Python solution for the problem:
def count_consonants_and_length(input_string):
# Initialize count of consonants
consonants_count = 0
# Define a list of consonants
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
# Convert the string to lower case to handle case insensitivity
input_string = input_string.lower()
# Iterate over each character in the string
for char in input_string:
# If the character is a consonant, increment the count
if char in consonants:
consonants_count += 1
# Return the length of the string and the count of consonants
return len(input_string), consonants_count
# Test the function
input_string = "write a program"
length, consonants_count = count_consonants_and_length(input_string)
print(length, consonants_count)
This program defines a function count_consonants_and_length that takes a string as input, counts the number of consonants in it, and returns the length of the string and the count of consonants. It then tests this function with the string "write a program". The function first initializes a count of consonants to 0 and defines a list of consonants. It then converts the input string to lower case to handle case insensitivity. It then iterates over each character in the string, and if the character is a consonant, it increments the count. Finally, it returns the length of the string and the count of consonants.
Similar Questions
Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
letter countWrite a C program that will analyze a line of text and will print the number of words, the number of consonants, and the number of vowels in the text.Testcase:Input:Programming is fun!Output:3 // Number of words:11 // Number of consonants:5 // Number of vowels:
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 in C to count the number of vowels and consonants in a string. You can use fgets to read the characters.
find the number of alphabet vowels and words in input using string
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.