How to Count the Number of Vowels and Consonants in a Sentence in Golang?


In this tutorial, we are going to see how to find the number of vowels and consonants in a sentence in Golang. If any character lies under the set {a, e, i, o , u} then that character is a vowel. Any character that does not lie in the above set is a consonant.

Explanation

Suppose we have a sentence “India have twenty eight states and eight union territories”. In this sentence, the characters which are bold are the vowels. So, the total vowels are 21 and consonants are 29.

Finding the count of vowels and consonants within the function

Algorithm

Step 1 − Declaring the sentence, vowelCount and, consonantCount variables.

Step 2 − Initializing the variables

Step 3 − Run a for loop over the sentence to count the number of vowels and consonants.

Step 4 − Printing the result.

Example 1

In this example, we are going to find the count of vowels and the consonants within the function.

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) }

Output

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

Finding the count of vowels and consonants in the separate function

Algorithm

Step 1 − Declaring the sentence, vowelCount and, consonantCount variables.

Step 2 − Initializing the variables

Step 3 − Call the function that is counting the number of vowels and consonants and returning them.

Step 4 − Printing the result.

Example 2

In this example, we are going to find the count of vowels and, the consonants in the separate function.

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) }

Output

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

Conclusion

These are the two ways to find the number of vowels and consonants in a sentence in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.

Updated on: 02-Sep-2022

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements