- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
In this article, we will understand how to count the vowels and consonants in Kotlin. Alphabets that include the following are called Vowels −
‘a’ ‘e’ ‘i’ ‘o’ ‘u’
All other alphabets are called Consonants.
Suppose our input is −
Hello, my name is Charlie
The desired output would be −
The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 12
Algorithm
Step 1 − Start
Step 2 − Declare two integers: vowelsCount, consonantsCount and a string input
Step 3 − Define the values
Step 4 − Run a for-loop, check each letter whether it is a consonant or a vowel. Increment the respective integer. Store the value.
Step 5 − Display the result
Step 6 − Stop
Example 1
In this example, we will count the Number of Vowels and Consonants in a Sentence using a for loop. First, declare and set the input string
var myInput = "Hello, my name is Charlie"
Now, set the variables for vowels and consonants and initialize to zero
var vowelsCount = 0 var consonantsCount = 0
Count the vowels and consonants
myInput = myInput.toLowerCase() for (i in 0..myInput.length - 1) { val ch = myInput[i] if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowelsCount } else if (ch in 'a'..'z') { ++consonantsCount } }
Let us now see the complete example to count the number of vowels and consonants using a for loop
fun main() { var myInput = "Hello, my name is Charlie" var vowelsCount = 0 var consonantsCount = 0 println("The statement is defined as: $myInput ") myInput = myInput.toLowerCase() for (i in 0..myInput.length - 1) { val ch = myInput[i] if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowelsCount } else if (ch in 'a'..'z') { ++consonantsCount } } println("The vowel count is: $vowelsCount") println("The consonants count is: $consonantsCount") }
Output
The statement is defined as: Hello, my name is Charlie The vowel count is: 8 The consonants count is: 12
Example 2
In this example, we will to count the Number of Vowels and Consonants in a Sentence −
fun main() { var myInput = "Hello, my name is Charlie" println("The statement is defined as: $myInput ") count(myInput) } fun count(input: String) { var myInput = input var vowelsCount = 0 var consonantsCount = 0 myInput = myInput.toLowerCase() for (i in 0..myInput.length - 1) { val ch = myInput[i] if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowelsCount } else if (ch in 'a'..'z') { ++consonantsCount } } println("The vowel count is: $vowelsCount") println("The consonants count is: $consonantsCount") }
Output
The statement is defined as: Hello, my name is Charlie The vowel count is: 8 The consonants count is: 12
- Related Articles
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?
- C# Program to count number of Vowels and Consonants in a string
- Java program to count the number of consonants in a given sentence
- Java program to count the number of vowels in a given sentence
- How to count number of vowels and consonants in a string in C Language?
- C Program to count vowels, digits, spaces, consonants using the string concepts
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- Frequency of vowels and consonants in JavaScript
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Alternating Vowels and Consonants in C/C++
- Validating alternating vowels and consonants in JavaScript
