Go Program to Replace a Character in a String

Introduction

Replacing a character in a string is a common task in text processing. It involves searching for all occurrences of a specific character and replacing them with another character. This guide will demonstrate how to write a Go program that replaces a character in a given string.

Problem Statement

Create a Go program that:

  • Prompts the user to enter a string.
  • Prompts the user to enter the character to be replaced and the replacement character.
  • Replaces all occurrences of the specified character with the replacement character.
  • Displays the modified string.

Example:

  • Input:
    • String: "Hello, World!"
    • Character to Replace: 'l'
    • Replacement Character: 'x'
  • Output: "Hexxo, Worxd!"

Solution Steps

  1. Import the fmt and strings Packages: Use import "fmt" for formatted I/O operations and import "strings" for string manipulation functions.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the String: Use fmt.Scanln or fmt.Scanf to take input from the user for the string.
  4. Input the Characters: Use fmt.Scanln or fmt.Scanf to take input from the user for the character to be replaced and the replacement character.
  5. Replace the Character: Use the strings.ReplaceAll function to replace all occurrences of the specified character.
  6. Display the Modified String: Use fmt.Println to display the modified string.

Go Program

package main

import (
    "fmt"
    "strings"
)

/**
 * Go Program to Replace a Character in a String
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare variables to hold the input string and characters
    var input string
    var oldChar, newChar string

    // Step 2: Prompt the user to enter a string
    fmt.Print("Enter a string: ")
    fmt.Scanln(&input)

    // Step 3: Prompt the user to enter the character to be replaced
    fmt.Print("Enter the character to be replaced: ")
    fmt.Scanln(&oldChar)

    // Step 4: Prompt the user to enter the replacement character
    fmt.Print("Enter the replacement character: ")
    fmt.Scanln(&newChar)

    // Step 5: Replace the character using strings.ReplaceAll
    modifiedString := strings.ReplaceAll(input, oldChar, newChar)

    // Step 6: Display the modified string
    fmt.Println("Modified string:", modifiedString)
}

Explanation

Step 1: Declare Variables

  • The variables input, oldChar, and newChar are declared to store the user’s input string, the character to be replaced, and the replacement character, respectively.

Step 2: Input the String

  • The program prompts the user to enter a string using fmt.Print. The fmt.Scanln function reads the input and stores it in the input variable.

Step 3: Input the Character to Be Replaced

  • The program prompts the user to enter the character they want to replace. This character is stored in the oldChar variable.

Step 4: Input the Replacement Character

  • The program prompts the user to enter the replacement character, which is stored in the newChar variable.

Step 5: Replace the Character

  • The strings.ReplaceAll function is used to replace all occurrences of oldChar with newChar in the input string. The result is stored in modifiedString.

Step 6: Display the Modified String

  • The program prints the modified string using fmt.Println.

Output Example

Example 1:

Enter a string: Hello, World!
Enter the character to be replaced: l
Enter the replacement character: x
Modified string: Hexxo, Worxd!

Example 2:

Enter a string: Go is great!
Enter the character to be replaced: o
Enter the replacement character: a
Modified string: Ga is great!

Conclusion

This Go program demonstrates how to replace a character in a string using the strings.ReplaceAll function. It covers basic programming concepts such as string manipulation, input/output operations, and handling user input. This example is useful for beginners learning Go programming and understanding how to modify and work with strings.

Leave a Comment

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

Scroll to Top