Go Program to Find the First Non-Repeated Character in a String

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

  1. Import the fmt Package: Use import "fmt" for formatted I/O operations.
  2. Write a Function to Find the First Non-Repeated Character: Implement a function that uses a map to track character frequencies.
  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 Find the First Non-Repeated Character: Use the function to determine the first non-repeated character.
  6. Display the Result: Use fmt.Println to 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 firstNonRepeatedChar function 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 true if such a character is found. If no non-repeated character exists, it returns a space character ' ' and false.

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 Find the First Non-Repeated Character

  • The program calls firstNonRepeatedChar to 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.

Leave a Comment

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

Scroll to Top