如何在 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 - 打印结果。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例 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 的信息,您可以浏览这些 教程。
广告