Go Program to Count Vowels and Consonants
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
How to Think About It
Algorithm
Code
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.
Read input
Input string is "hello"
Initialize counters
vowels = 0, consonants = 0
Loop through characters
Characters: 'h', 'e', 'l', 'l', 'o'
Check 'h'
'h' is letter and consonant, consonants = 1
Check 'e'
'e' is letter and vowel, vowels = 1
Check 'l'
'l' is letter and consonant, consonants = 2
Check second 'l'
'l' is letter and consonant, consonants = 3
Check 'o'
'o' is letter and vowel, vowels = 2
Print result
Vowels: 2, Consonants: 3
| Character | IsLetter | IsVowel | Vowels Count | Consonants Count |
|---|---|---|---|---|
| h | true | false | 0 | 1 |
| e | true | true | 1 | 1 |
| l | true | false | 1 | 2 |
| l | true | false | 1 | 3 |
| o | true | true | 2 | 3 |
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
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) }
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) }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch-case with unicode | O(n) | O(1) | Simple and Unicode-safe |
| Map lookup | O(n) | O(1) extra for map | Fast vowel checks, easy to extend |
| ASCII bitwise check | O(n) | O(1) | Fast for ASCII only, less Unicode support |
unicode.IsLetter to handle letters correctly including Unicode characters.