0
0
GoProgramBeginner · 2 min read

Go Program to Count Vowels and Consonants

Use a Go program that loops through each character of a string, checks if it is a vowel with switch or if, and counts vowels and consonants separately; for example, iterate over the string with for _, ch := range input and increment counters accordingly.
📋

Examples

Inputhello
OutputVowels: 2, Consonants: 3
InputGoLang
OutputVowels: 2, Consonants: 4
Input123!@#
OutputVowels: 0, Consonants: 0
🧠

How to Think About It

To count vowels and consonants, read each character of the input string one by one. Check if the character is a letter. If it is, check if it is a vowel by comparing it to vowels like 'a', 'e', 'i', 'o', 'u' in both lowercase and uppercase. If it matches, increase the vowel count. Otherwise, increase the consonant count. Ignore characters that are not letters.
📐

Algorithm

1
Get the input string from the user.
2
Initialize two counters: one for vowels and one for consonants.
3
Loop through each character in the string.
4
Check if the character is a letter.
5
If it is a vowel, increment the vowel counter.
6
If it is a consonant, increment the consonant counter.
7
After the loop, print the counts of vowels and consonants.
💻

Code

go
package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter a string: ")
	input, _ := reader.ReadString('\n')

	vowels, consonants := 0, 0
	for _, ch := range input {
		if unicode.IsLetter(ch) {
			switch unicode.ToLower(ch) {
			case 'a', 'e', 'i', 'o', 'u':
				vowels++
			default:
				consonants++
			}
		}
	}

	fmt.Printf("Vowels: %d, Consonants: %d\n", vowels, consonants)
}
🔍

Dry Run

Let's trace the input "hello" through the code.

1

Read input

Input string is "hello"

2

Initialize counters

vowels = 0, consonants = 0

3

Loop through characters

Characters: 'h', 'e', 'l', 'l', 'o'

4

Check 'h'

'h' is letter and consonant, consonants = 1

5

Check 'e'

'e' is letter and vowel, vowels = 1

6

Check 'l'

'l' is letter and consonant, consonants = 2

7

Check second 'l'

'l' is letter and consonant, consonants = 3

8

Check 'o'

'o' is letter and vowel, vowels = 2

9

Print result

Vowels: 2, Consonants: 3

CharacterIsLetterIsVowelVowels CountConsonants Count
htruefalse01
etruetrue11
ltruefalse12
ltruefalse13
otruetrue23
💡

Why This Works

Step 1: Check if character is a letter

We use unicode.IsLetter to ignore spaces, numbers, and symbols so only letters count.

Step 2: Identify vowels

We convert letters to lowercase with unicode.ToLower and check if they are one of 'a', 'e', 'i', 'o', 'u'.

Step 3: Count vowels and consonants

If a letter is a vowel, increase vowel count; otherwise, increase consonant count.

🔄

Alternative Approaches

Using map for vowels
go
package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode"
)

func main() {
	vowelsMap := map[rune]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true}
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter a string: ")
	input, _ := reader.ReadString('\n')

	vowels, consonants := 0, 0
	for _, ch := range input {
		if unicode.IsLetter(ch) {
			if vowelsMap[unicode.ToLower(ch)] {
				vowels++
			} else {
				consonants++
			}
		}
	}

	fmt.Printf("Vowels: %d, Consonants: %d\n", vowels, consonants)
}
Using a map makes vowel checking faster and easier to extend but uses extra memory.
Counting only ASCII letters
go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter a string: ")
	input, _ := reader.ReadString('\n')

	vowels, consonants := 0, 0
	for _, ch := range input {
		if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') {
			switch ch | 32 { // convert uppercase to lowercase
			case 'a', 'e', 'i', 'o', 'u':
				vowels++
			default:
				consonants++
			}
		}
	}

	fmt.Printf("Vowels: %d, Consonants: %d\n", vowels, consonants)
}
This approach uses bitwise OR to convert uppercase to lowercase and only works for ASCII letters.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops once through the input string of length n, checking each character, so it runs in O(n) time.

Space Complexity

Only a few counters and variables are used, so space complexity is O(1), constant space.

Which Approach is Fastest?

Using a map for vowels can be slightly faster for large inputs but uses extra memory; the switch-case method is simple and efficient for most cases.

ApproachTimeSpaceBest For
Switch-case with unicodeO(n)O(1)Simple and Unicode-safe
Map lookupO(n)O(1) extra for mapFast vowel checks, easy to extend
ASCII bitwise checkO(n)O(1)Fast for ASCII only, less Unicode support
💡
Use unicode.IsLetter to handle letters correctly including Unicode characters.
⚠️
Counting non-letter characters like spaces or punctuation as consonants or vowels.