Home >Backend Development >Golang >Replace repeated substrings in a string by replacing only one of them

Replace repeated substrings in a string by replacing only one of them

PHPz
PHPzforward
2024-02-09 08:15:291035browse

Replace repeated substrings in a string by replacing only one of them

php Xiaobian Yuezi replaces repeated substrings in a string and only replaces one of them. It is a common problem. In string processing, sometimes we need to replace repeated substrings in a string, but we do not want to replace all repeated substrings, but only one of them. This can retain other identical substrings while achieving the replacement effect we want. In PHP, we can achieve this requirement with the help of some built-in functions and regular expressions. Below, we will introduce in detail how to implement the method of replacing only one of the repeated substrings in the string.

Question content

I am making a simple executioner game using go, but I encountered an error, the unknown_string string (will be shown in the code) has _ characters, so guess the letters The other players can see the length of the word, and whenever a player enters the correct letter, I want it to replace the nth (n depends on the index where the letter is found in the word) _ character with that letter, this has been successful, but there are one question. If a word has two repeated letters, only one of them is replaced.

I created a separate function called iterate (this function was created because I wanted to avoid nested code) to iterate over all indices. But it doesn't work, the code is as follows:

package main

import (
    "fmt"
    "strings"
)

var input string
var word string
var unknown_word []string
var unknown_word_string string
var index int
var guesses int
var change_unknown_word_string []byte

func main() {
    fmt.Println("Welcome to hangman")
    fmt.Println("Player 1, choose a word!")
    fmt.Scan(&word)
    for i := 1; i <= len(word); i++ {
        unknown_word = append(unknown_word, "_")
    }
    unknown_word_string = strings.Join(unknown_word, "")
    for {
        fmt.Println("Player 2, guess a letter or a word")
        fmt.Println(unknown_word_string)
        fmt.Scan(&input)
        if guesses == 6 {
            fmt.Println("Player 2 lost! Player 1's word was: ", word)
            break
        } else if unknown_word_string == input {
            fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
        }
        if strings.Contains(word, input) && word != input {
            index = strings.Index(word, input)
            iterate()
            fmt.Println("You guessed a letter!")
        } else if word == input {
            fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the whole word!")
            break
        } else {
            fmt.Println("Nothing found")
            guesses++
        }
    }
}
func iterate() {
    change_unknown_word_string = []byte(unknown_word_string)
    for i := 0; i < len(change_unknown_word_string); i++ {
        if change_unknown_word_string[i] == change_unknown_word_string[index] {
            change_unknown_word_string[i] = []byte(input)[0]
        }
    }
    unknown_word_string = string(change_unknown_word_string)
}

Workaround

Comparisonif change_unknown_word_string[i] == change_unknown_word_string[index] doesn't make any sense since unknown_word_string is obviously included in these positions _.

You should compare word[i] == input[0] in the loop.

But please note that converting string to a byte array does not support unicode. It's better to use runes (unicode code points) so you're no longer limited to latin1 characters.

func iterate() {
    needle := []rune(input)[0]
    haystack := []rune(word)
    buf := []rune(unknown_word_string)
    for i := range haystack {
        if haystack[i] == needle {
            buf[i] = needle
        }
    }
    unknown_word_string = string(buf)
}

Note: This comparison is wrong

if unknown_word_string == input {
            fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
        }

It should be ifunknown_word_string == word and come after the call to iterate().

The above is the detailed content of Replace repeated substrings in a string by replacing only one of them. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete