Golang 如何统计句子中的元音和辅音数量

Golang 如何统计句子中的元音和辅音数量

在本教程中,我们将看到如何在Golang中找到句子中的元音和辅音数量。如果任何字符属于集合{a,e,i,o,u},那么该字符是一个元音。任何不属于上述集合的字符都是辅音。

解释

假设我们有一个句子“印度有二十八个邦和八个联邦属地”。在这个句子中,加粗的字符是元音。所以,总共有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 - 声明句子、元音计数和辅音计数变量。

步骤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语言的知识,你可以探索这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程