[英]Finding the last occurrence of a string in a sentence in C
我正在练习,我遇到了一个练习。 练习说我要手动编写一个函数来查找字符串中最后一次出现的索引。 现在,我知道之前可能已经问过这个问题,但我找不到我的代码有什么问题。 它适用于几乎所有情况,但当单词最后一次出现在字符串的开头时则不然。
我的尝试:我使用指针来存储我们正在寻找的句子和单词结尾的地址。 然后我使用了一个 while 循环来遍历字符串。 如果当前字符与我们正在搜索的单词的最后一个字符匹配,我们将进入另一个 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。
对于初学者,函数的参数应具有限定符 const 并且其返回类型应为size_t
或ptrdiff_t
。
例如
ptrdiff_t find_last( const char *str, const char *word );
在任何情况下,该函数至少应声明为
int find_last( const char *str, const char *word );
该函数应模拟标准 C 函数strstr
的行为。 也就是说,当第二个参数是一个空字符串时,函数应该返回 0。
如果其中一个参数是空字符串,则由于这些语句,您的函数具有未定义的行为
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--;
^^^^
如果str
指向的str
只包含一个符号,那么您的函数将返回 -1,因为循环的条件
while(p != str) {
独立于两个字符串是否彼此相等而评估为 false。
这个循环
while(*s == *t) {
s--;
t--;
}
再次可以调用未定义的行为,因为可以访问字符串字之前的内存。
而这个声明
if(t == word-1) return s-str+1;
也可以通过同样的原因调用未定义的行为。
可以定义该函数,如下面的演示程序所示。
#include <stdio.h>
int find_last( const char *str, const char *word )
{
const char *p = str;
int found = !*word;
if ( !found )
{
while ( *p ) ++p;
const char *q = word;
while ( *q ) ++q;
while ( !found && !( p - str < q - word ) )
{
const char *s = p;
const char *t = q;
while ( t != word && *( s - 1 ) == *( t - 1) )
{
--s;
--t;
}
found = t == word;
if ( found ) p = s;
else --p;
}
}
return found ? p - str : -1;
}
int main(void)
{
const char *str = "";
const char *word = "";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
word = "A";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
str = "A";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
str = "ABA";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
str = "ABAB";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
str = "ABCDEF";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
str = "ABCDEF";
word = "BC";
printf( "find_last( str, word ) == %d\n", find_last( str, word ) );
return 0;
}
程序输出是
find_last( str, word ) == 0
find_last( str, word ) == -1
find_last( str, word ) == 0
find_last( str, word ) == 2
find_last( str, word ) == 2
find_last( str, word ) == 0
find_last( str, word ) == 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.