Go Program to Count the Frequency of Each Character in a String

Introduction

Counting the frequency of each character in a string is a common task in text processing. This involves iterating through the string and tracking how many times each character appears. This guide will demonstrate how to write a Go program to count the frequency of each character in a given string.

Problem Statement

Create a Go program that:

  • Prompts the user to enter a string.
  • Counts the frequency of each character in the string.
  • Displays the frequency of each character.

Example:

  • Input: "hello"
  • Output:
    h: 1
    e: 1
    l: 2
    o: 1
    

Solution Steps

  1. Import the fmt Package: Use import "fmt" for formatted I/O operations.
  2. Write a Function to Count Character Frequencies: Implement a function that uses a map to track the frequency of each character.
  3. Write the Main Function: Define the main function, which is the entry point of every Go program.
  4. Prompt the User to Enter a String: Use fmt.Scanln or fmt.Scanf to take input from the user.
  5. Call the Function to Count Character Frequencies: Use the function to count the frequency of each character.
  6. Display the Frequency of Each Character: Use fmt.Println to display the character frequencies.

Go Program

package main

import "fmt"

// Step 2: Implement a function to count the frequency of each character in a string
func countCharacterFrequency(s string) map[rune]int {
    frequencyMap := make(map[rune]int)

    // Iterate through the string and count each character's frequency
    for _, char := range s {
        frequencyMap[char]++
    }

    return frequencyMap
}

/**
 * Go Program to Count the Frequency of Each Character in a String
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 4: Prompt the user to enter a string
    var input string
    fmt.Print("Enter a string: ")
    fmt.Scanln(&input)

    // Step 5: Call the function to count the frequency of each character
    frequencyMap := countCharacterFrequency(input)

    // Step 6: Display the frequency of each character
    fmt.Println("Character frequencies:")
    for char, count := range frequencyMap {
        fmt.Printf("%c: %d\n", char, count)
    }
}

Explanation

Step 2: Implement a Function to Count the Frequency of Each Character in a String

  • The countCharacterFrequency function performs the following steps:
    • It initializes an empty map frequencyMap to store the frequency of each character.
    • It iterates through the string, using the range keyword to access each character as a rune.
    • For each character, it increments its count in the frequencyMap.
    • The function returns the frequencyMap, which contains the frequency of each character in the string.

Step 4: Prompt the User to Enter a String

  • The program prompts the user to enter a string using fmt.Print and reads the input using fmt.Scanln.

Step 5: Call the Function to Count Character Frequencies

  • The program calls countCharacterFrequency to determine the frequency of each character in the input string.

Step 6: Display the Frequency of Each Character

  • The program prints each character and its frequency using a for loop to iterate over the frequencyMap.

Output Example

Example 1:

Enter a string: hello
Character frequencies:
h: 1
e: 1
l: 2
o: 1

Example 2:

Enter a string: go programming
Character frequencies:
g: 3
o: 2
 : 1
p: 1
r: 2
a: 1
m: 2
i: 1
n: 1

Example 3:

Enter a string: aabbcc
Character frequencies:
a: 2
b: 2
c: 2

Example 4:

Enter a string: abcabcabc
Character frequencies:
a: 3
b: 3
c: 3

Conclusion

This Go program demonstrates how to count the frequency of each character in a string using a map. It covers basic programming concepts such as string manipulation, maps, and iteration in Go. This example is useful for beginners learning Go programming and understanding how to perform common string-related operations efficiently.

Leave a Comment

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

Scroll to Top