IPS5-Pointer5Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Question
IPS5-Pointer5
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 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
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 C program to read a sentence through keyboard and to display the count ofwhite spaces in the given sentence.
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.
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.