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
- Import the fmt Package: Use
import "fmt"for formatted I/O operations. - Write a Function to Count Character Frequencies: Implement a function that uses a map to track the frequency of each character.
- Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Prompt the User to Enter a String: Use
fmt.Scanlnorfmt.Scanfto take input from the user. - Call the Function to Count Character Frequencies: Use the function to count the frequency of each character.
- Display the Frequency of Each Character: Use
fmt.Printlnto 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
countCharacterFrequencyfunction performs the following steps:- It initializes an empty map
frequencyMapto store the frequency of each character. - It iterates through the string, using the
rangekeyword to access each character as arune. - 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.
- It initializes an empty map
Step 4: Prompt the User to Enter a String
- The program prompts the user to enter a string using
fmt.Printand reads the input usingfmt.Scanln.
Step 5: Call the Function to Count Character Frequencies
- The program calls
countCharacterFrequencyto 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
forloop to iterate over thefrequencyMap.
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.