Introduction
Finding the first non-repeated character in a string is a common problem in string manipulation. The goal is to identify the first character that does not repeat anywhere else in the string. This guide will demonstrate how to write a Go program to solve this problem.
Problem Statement
Create a Go program that:
- Prompts the user to enter a string.
- Finds the first non-repeated character in the string.
- Displays the first non-repeated character, or a message indicating that no such character exists.
Example:
- Input:
"swiss" - Output:
The first non-repeated character is "w"
Solution Steps
- Import the fmt Package: Use
import "fmt"for formatted I/O operations. - Write a Function to Find the First Non-Repeated Character: Implement a function that uses a map to track character frequencies.
- 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 Find the First Non-Repeated Character: Use the function to determine the first non-repeated character.
- Display the Result: Use
fmt.Printlnto display the first non-repeated character or indicate that no such character exists.
Go Program
package main
import "fmt"
// Step 2: Implement a function to find the first non-repeated character
func firstNonRepeatedChar(s string) (rune, bool) {
charCount := make(map[rune]int)
// Count the occurrences of each character
for _, char := range s {
charCount[char]++
}
// Find the first character with a count of 1
for _, char := range s {
if charCount[char] == 1 {
return char, true
}
}
return ' ', false
}
/**
* Go Program to Find the First Non-Repeated 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 find the first non-repeated character
char, found := firstNonRepeatedChar(input)
// Step 6: Display the result
if found {
fmt.Printf("The first non-repeated character is \"%c\"\n", char)
} else {
fmt.Println("There is no non-repeated character in the string.")
}
}
Explanation
Step 2: Implement a Function to Find the First Non-Repeated Character
- The
firstNonRepeatedCharfunction performs the following steps:- It uses a map (
charCount) to track the frequency of each character in the string. - It iterates through the string to count the occurrences of each character.
- It then iterates through the string a second time to find the first character with a count of 1, indicating it is non-repeated.
- The function returns the first non-repeated character and a boolean
trueif such a character is found. If no non-repeated character exists, it returns a space character' 'andfalse.
- It uses a 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 Find the First Non-Repeated Character
- The program calls
firstNonRepeatedCharto determine the first non-repeated character.
Step 6: Display the Result
- The program prints the first non-repeated character using
fmt.Printf. If no non-repeated character exists, it prints a message indicating that.
Output Example
Example 1:
Enter a string: swiss
The first non-repeated character is "w"
Example 2:
Enter a string: racecar
The first non-repeated character is "e"
Example 3:
Enter a string: aabbcc
There is no non-repeated character in the string.
Example 4:
Enter a string: abcabcde
The first non-repeated character is "d"
Conclusion
This Go program demonstrates how to find the first non-repeated character in a string using a map to track character frequencies. 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 solve common string-related problems efficiently.