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'
- String:
- Output:
"Hexxo, Worxd!"
Solution Steps
- Import the fmt and strings Packages: Use
import "fmt"for formatted I/O operations andimport "strings"for string manipulation functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Input the String: Use
fmt.Scanlnorfmt.Scanfto take input from the user for the string. - Input the Characters: Use
fmt.Scanlnorfmt.Scanfto take input from the user for the character to be replaced and the replacement character. - Replace the Character: Use the
strings.ReplaceAllfunction to replace all occurrences of the specified character. - Display the Modified String: Use
fmt.Printlnto 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, andnewCharare 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. Thefmt.Scanlnfunction reads the input and stores it in theinputvariable.
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
oldCharvariable.
Step 4: Input the Replacement Character
- The program prompts the user to enter the replacement character, which is stored in the
newCharvariable.
Step 5: Replace the Character
- The
strings.ReplaceAllfunction is used to replace all occurrences ofoldCharwithnewCharin the input string. The result is stored inmodifiedString.
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.