【发布时间】:2020-02-19 11:24:41
【问题描述】:
我正在练习,我遇到了一个练习。练习说我要手动编写一个函数来查找字符串中最后一次出现的索引。现在,我知道这可能以前被问过,但我找不到我的代码有什么问题。它适用于几乎所有实例,但当单词最后一次出现在字符串的开头时则不适用。
我的尝试:我使用指针来存储句子和我们正在寻找的单词的 ends 的地址。然后我使用了一个while循环来遍历字符串。如果当前字符与我们正在搜索的单词的 last 字符匹配,我们进入另一个比较两者的 while 循环。如果指向单词开头的指针和我们用来遍历单词的指针相等,则找到该单词。
这里有一些代码:
#include <stdio.h>
int find_last( char *str, char *word)
{
char *p, *q;
char *s, *t;
p=str; /* Pointer p now points to the last character of the sentence*/
while(*p!='\0') p++;
p--;
q = word;
while(*q!='\0') q++; /* Pointer q now points to the last character of the word*/
q--;
while(p != str) {
if(*p == *q) {
s=p; /* if a matching character is found, "s" and "t" are used to iterate through */
/* the string and the word, respectively*/
t=q;
while(*s == *t) {
s--;
t--;
}
if(t == word-1) return s-str+1; /* if pointer "t" is equal by address to pointer word-1, we have found our match. return s-str+1. */
}
p--;
}
return -1;
}
int main()
{
char arr[] = "Today is a great day!";
printf("%d", find_last(arr, "Today"));
return 0;
}
所以,这段代码应该返回0,但它返回-1。
它适用于我测试的所有其他实例!在 CodeBlocks 中运行时,输出符合预期 (0),但使用任何其他在线 IDE,我发现输出仍然是 -1。
【问题讨论】:
-
while(*s == *t) { s--; t--; }- 你不能那样做,你不知道什么时候结束。 -
@Srilakshmikanthanp 等等,现在当我在 CodeBlocks 上尝试它时,它也适用于我。但是,当使用我的大学提供的在线云 IDE 时,它无法按预期工作!
-
我还希望那些对问题投反对票的人提供这样做的理由。我遵循了为提问而制定的所有准则,据我所知,我是准确且非常清楚的。
-
@johndoe 就我而言,我认为这项任务对于初学者来说并不容易。 SO也不明白为什么您的问题被否决了,尽管您描述了问题并提供了演示问题的代码。我赞成你的问题。:)
-
@VladfromMoscow 嘿,非常感谢!向新人提问会变得非常令人沮丧。再次感谢您!
标签: c arrays find c-strings substr