[问题描述]补充完整函数strrindex(s,t),其功能用于返回字符串t在字符串s中最右边出现的位置,该位置从0开始计数,如果s中不含有t,那么返回-1;在你编写的程序中,使用strrindex(s,t)函数输入t,s,输出t在s最右边的位置
时间: 2024-02-24 14:56:39 浏览: 18
下面是完整的函数实现:
```c
int strrindex(char s[], char t[]) {
int i, j, k;
int pos = -1; // 初始化为-1,表示没有找到t在s中的位置
for (i = 0; s[i] != '\0'; i++) {
for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
; // 查找t在s中出现的位置
if (k > 0 && t[k] == '\0') // 如果找到了t在s中出现的位置
pos = i; // 更新pos
}
return pos;
}
```
函数中使用了两个循环,外层循环用于遍历s中的每一个字符,内层循环用于查找t在s中出现的位置。如果找到了t在s中出现的位置,就更新pos的值。最后返回pos的值即可。
以下是使用该函数的示例代码:
```c
#include <stdio.h>
int strrindex(char s[], char t[]);
int main() {
char s[] = "hello, world!";
char t[] = "l";
int pos = strrindex(s, t);
printf("%d\n", pos);
return 0;
}
```
输出结果为:
```
10
```
表示t在s中最右边出现的位置是10。