Golang 如何计算一个句子中元音和辅音的数量

Golang 如何计算一个句子中元音和辅音的数量

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

解释

假设我们有一个句子 “印度有28个州和8个联邦区”。在这个句子中,黑体字是元音。所以,元音共有21个,辅音共有29个。

在函数中寻找元音和辅音的数量

算法

第1步 - 声明句子、元音计数和辅音计数的变量。

第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中查找句子中元音和辅音数量的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用这个函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程