检测特定的字符串

huangapple go评论75阅读模式
英文:

Detecting a specific character string

问题

最近,我有一个学校项目让我苦苦挣扎了一个星期。
我真的需要帮助。

我的程序接受一个字符串输入,并将其修改为返回另一个字符串,其中包含大写或小写版本。到目前为止,它运行正常。但是当我输入:"This is so exciting (up, 2)"时,输出保持不变,而应该是"This is SO EXCITING"。
我的目标是能够处理这种情况:(up, n),即跟在"(up)"后面的单词必须大写。
下面是相关的函数。

提前感谢!

// modifyUp将(up)之前的单词转换为大写
func modifyUp(line string) string {
	words := strings.Fields(line)
	var numWords int
	var numbers int

	for i, word := range words {
		if strings.HasSuffix(word, "(up)") && i > 0 {
			// 将单词转换为大写
			numWords = 1
			if strings.Contains(word, ",") {
				// 处理逗号分隔的单词
				numbers, _ = strconv.Atoi(strings.TrimSuffix(strings.Split(word, ",")[1], ")"))
				word = strings.TrimSuffix(word, fmt.Sprintf(",%d)", numbers))
				numWords += numbers
			} else {
				word = strings.TrimSuffix(word, "(up)")
			}
			// 将前面的单词转换为大写
			for j := i - numWords + 1; j <= i; j++ {
				if j >= 0 {
					words[j] = strings.ToUpper(words[j])
				}
			}
			// 用原始单词替换修改后的单词
			words[i-numWords+1] = word
		}
	}
	return strings.Join(words, " ")
}
英文:

Recently, I had a school project that made me struggle for a week.
I really need help.

My program takes a string input and modifies it to return another string in its upper or lower case version, so far it works. But when I put in :
"This is so exciting (up, 2)" the output stays the same when it should be "This is SO EXCITING"
My point is to be able to handle the case : (up, n), knowing that the word followed by "(up)" must be capitalized.
If below you will find the concerned function.

Thanks in advance !

// modifyUp converts words before (up) to uppercase
func modifyUp(line string) string {
	words := strings.Fields(line)
	var numWords int
	var numbers int

	for i, word := range words {
		if strings.HasSuffix(word, &quot;(up)&quot;) &amp;&amp; i &gt; 0 {
			// Convert word(s) to uppercase
			numWords = 1
			if strings.Contains(word, &quot;,&quot;) {
				// Handle comma-separated words
				numbers, _ = strconv.Atoi(strings.TrimSuffix(strings.Split(word, &quot;,&quot;)[1], &quot;)&quot;))
				word = strings.TrimSuffix(word, fmt.Sprintf(&quot;,%d)&quot;, numbers))
				numWords += numbers
			} else {
				word = strings.TrimSuffix(word, &quot;(up)&quot;)
			}
			// Convert preceding word(s) to uppercase
			for j := i - numWords + 1; j &lt;= i; j++ {
				if j &gt;= 0 {
					words[j] = strings.ToUpper(words[j])
				}
			}
			// Replace modified word with original word
			words[i-numWords+1] = word
		}
	}
	return strings.Join(words, &quot; &quot;)

答案1

得分: 0

如评论中所提到的,您正在检查特定的(up),这将无法检测到(up, 2)。您可以在for循环的开头添加fmt.Println(word)来验证您正在检查的每个单词。对于This is so exciting (up, 2),您会得到以下结果:

This
is
so
exciting
(up,
2)

可以看到,找到了(up,而不是(up)。此外,2)与检查分开。

如果我们保持您当前的逻辑,可以进行以下快速修复:

func modifyUp(line string) string {
    words := strings.Fields(line)
    var numWords int

    for i, word := range words {
        if (strings.HasSuffix(word, "(up)") || strings.HasSuffix(word, "(up,") && i > 0 {
            // 将单词转换为大写
            numWords = 1
            if strings.Contains(word, ",") {
                var numbers int

                // 处理逗号分隔的单词
                numbers, _ = strconv.Atoi(strings.TrimRight(words[i+1], ")"))

                words[i] = ""
                words[i+1] = ""
                numWords = numbers
            } else {
                words[i] = ""
            }

            // 将前面的单词转换为大写
            for j := i - numWords; j <= i; j++ {
                words[j] = strings.ToUpper(words[j])
            }
        }
    }
    return strings.Join(words, " ")
}

我对代码进行了一些额外的更改,以使其在正确解析up后能正常工作。由于您已经在转换中替换了它,您还可以删除words[i-numWords+1] = word

希望对您有所帮助,如果您有任何后续问题,请告诉我。

英文:

As mentioned in the comment, you are checking for specifically (up) which will not detect (up, 2). An easy way to check this is that you can throw in an fmt.Println(word) at the start of the for loop to validate each word you're checking. In the case of This is so exciting (up, 2), you get

This
is
so
exciting
(up,
2)

As you can see, (up, is found instead of (up). On top of that, 2) is separated from the check.

If we're keeping the same logic that you currently have, a quick fix for this is as followed:

func modifyUp(line string) string {
    words := strings.Fields(line)
    var numWords int

    for i, word := range words {
	    if (strings.HasSuffix(word, &quot;(up)&quot;) || strings.HasSuffix(word, &quot;(up,&quot;)) &amp;&amp; i &gt; 0 {
		    // Convert word(s) to uppercase
		    numWords = 1
		    if strings.Contains(word, &quot;,&quot;) {
			    var numbers int

			    // Handle comma-separated words
			    numbers, _ = strconv.Atoi(strings.TrimRight(words[i+1], &quot;)&quot;))

			    words[i] = &quot;&quot;
			    words[i+1] = &quot;&quot;
			    numWords = numbers
		    } else {
			    words[i] = &quot;&quot;
		    }

		    // Convert preceding word(s) to uppercase
		    for j := i - numWords; j &lt;= i; j++ {
			    words[j] = strings.ToUpper(words[j])
		    }
	    }
    }
    return strings.Join(words, &quot; &quot;)
}

I made some extra changes to the code to get it to work once you parsed the up correctly. You can also drop words[i-numWords+1] = word since you're already replacing it in the conversion.

Hope this helps and let me know if you have any following up questions.

答案2

得分: -1

我终于找到了另一种方法

func modifyUp(line string) string {
	words := strings.Fields(line)
	//var numWords int
	var numbers int

	for i, word := range words {
		if word == "(up," && i < len(words)-1 {
			ok := splitWord(words[i+1])
			if ok {
				wr := words[i+1]
				numbers, _ = strconv.Atoi(wr[:len(wr)-1])
				words[i], words[i+1] = "", ""
				for j := i - 1; j >= 0; j-- {
					if numbers == 0 {
						break
					} else {
						words[j] = strings.ToUpper(words[j])
						numbers--
					}
				}
			}
			break
		}
		if word == "(up)" && i > 0 {
			words[i-1] = strings.ToUpper(words[i-1])
			words[i] = ""
		}
	}

	return strings.Join(words, " ")
}

感谢大家的贡献

英文:

I finally found another way

func modifyUp(line string) string {
	words := strings.Fields(line)
	//var numWords int
	var numbers int

	for i, word := range words {
		if word == &quot;(up,&quot; &amp;&amp; i &lt; len(words)-1 {
			ok := splitWord(words[i+1])
			if ok {
				wr := words[i+1]
				numbers, _ = strconv.Atoi(wr[:len(wr)-1])
				words[i], words[i+1] = &quot;&quot;, &quot;&quot;
				for j := i - 1; j &gt;= 0; j-- {
					if numbers == 0 {
						break
					} else {
						words[j] = strings.ToUpper(words[j])
						numbers--
					}
				}
			}
			break
		}
		if word == &quot;(up)&quot; &amp;&amp; i &gt; 0 {
			words[i-1] = strings.ToUpper(words[i-1])
			words[i] = &quot;&quot;
		}
	}

	return strings.Join(words, &quot; &quot;)
}

Thank all you for contibutions

huangapple
  • 本文由 发表于 2023年4月1日 12:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75904215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定