英文:
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, "(up)") && i > 0 {
// Convert word(s) to uppercase
numWords = 1
if strings.Contains(word, ",") {
// Handle comma-separated words
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)")
}
// Convert preceding word(s) to uppercase
for j := i - numWords + 1; j <= i; j++ {
if j >= 0 {
words[j] = strings.ToUpper(words[j])
}
}
// Replace modified word with original word
words[i-numWords+1] = word
}
}
return strings.Join(words, " ")
答案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, "(up)") || strings.HasSuffix(word, "(up,")) && i > 0 {
// Convert word(s) to uppercase
numWords = 1
if strings.Contains(word, ",") {
var numbers int
// Handle comma-separated words
numbers, _ = strconv.Atoi(strings.TrimRight(words[i+1], ")"))
words[i] = ""
words[i+1] = ""
numWords = numbers
} else {
words[i] = ""
}
// Convert preceding word(s) to uppercase
for j := i - numWords; j <= i; j++ {
words[j] = strings.ToUpper(words[j])
}
}
}
return strings.Join(words, " ")
}
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 == "(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, " ")
}
Thank all you for contibutions
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论