Swift程式:統計句子中母音和子音的個數
本教程將討論如何編寫一個Swift程式來統計句子中母音和子音的個數。
字母表包含26個字母,其中5個是母音,21個是子音。A、E、I、O和U被稱為母音,B、C、D、F、G、H、J、K、L、M、N、P、Q、R、S、T、V、W、X、Y、Z被稱為子音。
以下是演示:
輸入
假設我們的輸入是:
Entered String I like momos
輸出
期望輸出為:
Total number of Vowels - 6 Toal number of Consonants - 4
演算法
以下是演算法:
步驟1 - 建立函式。
步驟2 - 宣告兩個變數,初始值為0 - countVowel = 0 和 countConsonant = 0。這兩個變數儲存母音和子音的總數。
步驟3 - 宣告兩個變數,名為myVowel和myConsonant。myVowel儲存包含所有母音的字串,myConsonant儲存僅包含子音的字串。
步驟4 - 將輸入字串轉換為小寫。
let myString = mystr.lowercased()
步驟5 - 執行for迴圈檢查每個字元。如果字元是母音,則將countVowel加1。否則,檢查子音,如果是,則將countConsonant加1。
for i in myString { if myVowel.contains(i){ countVowel += 1 } else if myconsonant.contains(i){ countConsonant += 1 } }
步驟6 - 返回母音和子音的總數。
步驟7 - 呼叫函式並將輸入字串作為引數傳遞給函式。
步驟8 - 列印輸出。
示例1
以下程式演示如何統計句子中母音和子音的個數。
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)
輸出
Total number of vowels in the given statement are 9 Total number of Consonants in the given statement are 10
在上面的程式碼中,我們建立了一個名為findVowelConsonant()的函式來統計母音和子音的總數。在這個函式中,我們首先將輸入字串轉換為小寫。然後使用以下程式碼檢查字串的每個字元是否為母音和子音:
for i in myString { if myVowel.contains(i) { countVowel += 1 } else if myconsonant.contains(i) { countConsonant += 1 } }
如果字元是母音,則將"countVowel"加一。如果字元是子音,則將"countConsonant"加一。現在我們呼叫上述函式,並將字串“hello tutorialspoint”作為引數傳遞,顯示字串中母音和子音的總數,即母音=9,子音=10。
示例2
以下程式演示如何統計句子中母音和子音的個數。
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)
標準輸入
Please enter the string: I like Swift language
輸出
Total number of vowels are 9 Total number of Consonants are 11
在上面的程式碼中,我們建立了一個名為findVowelConsonant()的函式來統計母音和子音的總數。在這裡,我們從使用者那裡獲取輸入字串,並將此字串作為引數傳遞給函式,以查詢其中的母音和子音總數。例如,輸入字串 = “I like Swift language”,則母音總數 = 9,子音總數 = 9。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP