- 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
Swift Program to Count the Number of Vowels and Consonants in a Sentence
This tutorial will discuss how to write a Swift program to count the number of vowels and consonants in a sentence.
An alphabet contains 26 letters out of which 5 are vowels and 21 are consonants. A, E, I, O, and U are known as vowels and B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, Z are known as consonants.
Below is a demonstration of the same −
Input
Suppose our given input is −
Entered String I like momos
Output
The desired output would be −
Total number of Vowels - 6 Toal number of Consonants - 4
Algorithm
Following is the algorithm −
Step 1 − Create function.
Step 2 − Declare two variables with entail 0 value - countVowel = 0 and countConsonant = 0. Here these variable store the total count of vowels and consonants.
Step 3 − Declare two variables named as myVowel and my consonant. Here myVowel store a string which contain all the vowels whereas myconsonant store a string which contains only consonants.
Step 4 − Convert the input string to lowercase.
let myString = mystr.lowercased()
Step 5 − Run a for loop to check every character. If the character is vowel then increase the count of countVowel by 1. Otherwise check for the consonant and if yes then increase the count of countConsonant by 1.
for i in myString { if myVowel.contains(i){ countVowel += 1 } else if myconsonant.contains(i){ countConsonant += 1 } }
Step 6 − Return the total count of vowel and consonants.
Step 7 − Call the function and pass the input string to the function as a parameter.
Step 8 − Print output.
Example 1
The following program shows how to count the number of vowels and consonants in a sentence.
import Foundation import Glibc // Function to find the total number of vowels and Consonants func findVowelConsonant(mystr: String) ->(Vowels: Int, Consonant: Int){ var countVowel = 0 var countConsonant = 0 let myVowel = "aeious" let myconsonant = "bcdfghijklmnpqrtvwxyz" let myString = mystr.lowercased() for i in myString{ if myVowel.contains(i){ countVowel += 1 } else if myconsonant.contains(i){ countConsonant += 1 } } return (countVowel, countConsonant) } // Calling the function var res = findVowelConsonant(mystr: "hello tutorialspoint") // Displaying the total number of vowels and consonants // by accessing the parameters of the function // Using dot operator print("Total number of vowels in the given statement are ", res.Vowels) print("Total number of Consonants in the given statement are ", res.Consonant)
Output
Total number of vowels in the given statement are 9 Total number of Consonants in the given statement are 10
Here, in the above code, we create a function named as findVowelConsonant() to count the total number of vowel and consonants. In this function, first we convert the input string into lowercase. Then check every character of the string for the vowel and consonant using the following code −
for i in myString { if myVowel.contains(i) { countVowel += 1 } else if myconsonant.contains(i) { countConsonant += 1 } }
If the character is vowel then increase the count of "countVowel" by one. Or if the character is consonant then increase the count of "countConsonant" by one. Now we call the above function and pass "hello tutorialspoint" string as an argument and display the total number of vowels and consonants present in the string that are V-owel = 9 and Consonant = 10.
Example 2
The following program shows how to count the number of vowels and consonants in a sentence.
import Foundation import Glibc // Function to Find the total number of vowels and Consonants func findVowelConsonant(mystr: String) ->(Vowels: Int, Consonant: Int){ var countVowel = 0 var countConsonant = 0 let myVowel = "aeious" let myconsonant = "bcdfghijklmnpqrtvwxyz" let myString = mystr.lowercased() for i in myString{ if myVowel.contains(i){ countVowel += 1 } else if myconsonant.contains(i){ countConsonant += 1 } } return (countVowel, countConsonant) } // Taking input from the user print("Please enter the string:") var nstr = String(readLine()!) // Calling the function var res = findVowelConsonant(mystr: nstr) // Displaying the total number of vowels and consonants // by accessing the parameters of the function // Using dot operator print("Total number of vowels are ", res.Vowels) print("Total number of Consonants are ", res.Consonant)
STDIN Input
Please enter the string: I like Swift language
Output
Total number of vowels are 9 Total number of Consonants are 11
Here, in the above code, we create a function named as findVowelConsonant() to count the total number of vowel and consonants. Here we take the input string from the user and pass this string to the function as an argument to find the total number of vowels and consonants in it. For example, the input string = “I like Swift language”, so the total number of Vowels = 9 and Consonants = 9.
- Related Articles
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin 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
