Menu Close

Go – Program to count vowels in a string in go

Here, we will see Program to count vowels in a string in go. Given below, you can find algorithm and program.

Algorithm:

  • Get the string input from user
  • Count the vowels from string
  • Return the count
Input: char = "Hello Go"
Output: 3

Input: char = "This is golang code"
Output: 6

Input: char = "By"
Output: 0

Code:

package main

import (

  "fmt"

)

func is_vowel(char rune) bool  {

  if ((char == 'a') || (char == 'e') || (char == 'i') ||
      (char == 'o') || (char == 'u')) {

    return true

  } else {

    return false

  }

}

func count_vowels(str string) int {

  count := 0

  for _, char := range str {

    if (is_vowel(char)) {

      count = count + 1

    }

  }

  return count

}

func main() {

  x := count_vowels("Hello Go")

  fmt.Println(x)



  x = count_vowels("This is golang code")

  fmt.Println(x)



  x = count_vowels("By")

  fmt.Println(x)

}

Output:

3

6

0

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, golang program

Leave a Reply

Your email address will not be published.

Contact Us