如何在 Golang 中統計句子中母音和子音的數量?


在本教程中,我們將瞭解如何在 Golang 中查詢句子中母音和子音的數量。如果任何字元位於集合 {a, e, i, o , u} 中,則該字元為母音。任何不在上述集合中的字元都是子音。

解釋

假設我們有一個句子“India have twenty eight states and eight union territories”。在這個句子中,加粗的字元是母音。因此,母音總數為 21,子音為 29。

在函式內查詢母音和子音的計數

演算法

步驟 1 - 宣告句子、vowelCount 和 consonantCount 變數。

步驟 2 - 初始化變數

步驟 3 - 對句子執行 for 迴圈以計算母音和子音的數量。

步驟 4 - 列印結果。

示例 1

在這個例子中,我們將找到函式內母音和子音的計數。

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the variable sentence of string type // which stores the sentence in which we have to // count the vowels and the Consonants var sentence string // declaring the variables to store the count // of vowels and Consonants var vowelsCount, consonantCount int // initializing the variable sentence sentence = "India have twenty eight states and eight union territories" // initializing the variable vowelsCount vowelsCount = 0 // initializing the variable ConsonantCount consonantCount = 0 fmt.Println("Program to find the count of vowels and consonants within the function.") // running a for loop over the string stored in the sentence variable for i := 0; i < len(sentence); i++ { // skipping the spaces in the sentence if sentence[i] == ' ' { continue } // comparing the current character with the vowels if sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' || sentence[i] == 'u' || sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] == 'I' || sentence[i] == 'O' || sentence[i] == 'U' { // increasing the count of vowelCount variable // if the current character is a vowel vowelsCount++ } else { // increasing the count of consonantCount variable // if current character is consonant consonantCount++ } } fmt.Println("Sentence:- \n", sentence) // printing the count of vowels and consonants fmt.Println("Result:- \nThe total number of vowels in the above sentence are", vowelsCount) fmt.Println("The total number of consonants in the above sentence are", consonantCount) }

輸出

Program to find the count of vowels and consonants within the function.
Sentence:-
 India have twenty eight states and eight union territories
Result:-
The total number of vowels in the above sentence are 21
The total number of consonants in the above sentence are 29

在單獨的函式中查詢母音和子音的計數

演算法

步驟 1 - 宣告句子、vowelCount 和 consonantCount 變數。

步驟 2 - 初始化變數

步驟 3 - 呼叫計算母音和子音數量並返回它們的函式。

步驟 4 - 列印結果。

示例 2

在這個例子中,我們將找到單獨函式中母音和子音的計數。

package main // fmt package provides the function to print anything import "fmt" // defining the function which has a parameter of string type // and as in Golang we can return more than one value at once // so here we are returning vowelCount and consonantCount together // (int, int) is the way to achieve the above thing func vowelsconsonantsCount(sentence string) (int, int) { // declaring the variables to store the count // of vowels and consonants var vowelsCount, consonantCount int // initializing the variable vowelsCount vowelsCount = 0 // initializing the variable consonantCount consonantCount = 0 // running a for loop over the string stored in the sentence variable for i := 0; i < len(sentence); i++ { // skipping the spaces in the sentence if sentence[i] == ' ' { continue } // comparing the current character with the vowels if sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' || sentence[i] == 'u' || sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] == 'I' || sentence[i] == 'O' || sentence[i] == 'U' { // increasing the count of vowelCount variable // if the current character is a vowel vowelsCount++ } else { // increasing the count of consonantCount variable // if current character is consonant consonantCount++ } } // returning the count of vowels and consonants return vowelsCount, consonantCount } func main() { // declaring the variable sentence of string type // which stores the sentence in which we have to // count the vowels and the consonants var sentence string // declaring the variables to store the count // of vowels and consonants var vowelsCount, consonantCount int // initializing the variable sentence sentence = "India have twenty eight states and eight union territories" fmt.Println("Program to find the count of vowels and consonants in the separate function.") // calling the function and storing the count of consonant and // vowels in the variable vowelsCount, consonantCount = vowelsconsonantsCount(sentence) fmt.Println("Sentence:-\n", sentence) // printing the count of vowels and consonants fmt.Println("Result:- \nThe total number of vowels in the above sentence are", vowelsCount) fmt.Println("The total number of consonants in the above sentence are", consonantCount) }

輸出

Program to find the count of vowels and consonants in the separate function.
Sentence:-
 India have twenty eight states and eight union territories
Result:-
The total number of vowels in the above sentence are 21
The total number of consonants in the above sentence are 29

結論

這兩種方法可以在 Golang 中查詢句子中母音和子音的數量。第二種方法在模組化和程式碼可重用性方面要好得多,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於 go 的資訊,您可以瀏覽這些 教程

更新於: 2022年9月2日

740 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.