Count the Number of Vowels in a Given String

This program counts the number of vowels (a, e, i, o, u) in a given string using the Go programming language. Below is the complete code with an explanation of its structure.

Go Program


// Package main defines the entry point for the program
package main

// Importing the fmt package for formatted I/O
import (
    "fmt"
    "strings"
)

// Function countVowels takes a string input and returns the count of vowels in it
func countVowels(s string) int {
    // Variable to hold the count of vowels
    count := 0

    // Convert the string to lowercase to make the function case-insensitive
    s = strings.ToLower(s)

    // Loop through each character in the string
    for _, char := range s {
        // Check if the character is a vowel
        if char == 'a' || char == 'e' || char == 'i' || char == 'o' || char == 'u' {
            // Increment the count if a vowel is found
            count++
        }
    }

    // Return the total count of vowels
    return count
}

// Main function is the entry point of the program
func main() {
    // Example string to count vowels
    str := "Hello, World!"

    // Call countVowels function and store the result
    result := countVowels(str)

    // Print the result
    fmt.Printf("The number of vowels in '%s' is: %d\n", str, result)
}
    

Explanation

  • Package main: Defines the entry point of the Go program.
  • Import fmt and strings: Imports necessary packages for formatted I/O and string manipulation.
  • Function countVowels: Takes a string as input, converts it to lowercase, and iterates through each character to count the vowels.
  • Variable count: Stores the number of vowels found in the string.
  • strings.ToLower(s): Converts the input string to lowercase to make vowel counting case-insensitive.
  • for _, char := range s: Iterates through each character in the string.
  • If condition: Checks if the character is a vowel and increments the count if true.
  • Return count: Returns the total count of vowels found in the string.
  • Function main: The entry point of the program that calls countVowels with an example string and prints the result.

Usage

To run the program, save the code in a file with a .go extension and execute it using the Go compiler:

$ go run filename.go

The program will output the number of vowels in the given string.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)