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.

Updated on: 18-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements