问题描述】补充完整函数strrindex(s,t),其功能用于返回字符串t在字符串s中最右边出现的位置.该位置从0开始计数,如果s中不含有t,那么返回-1;在你编写的程序中,使用strrindex(s,t)函数,输入t,s,输出t在s最右边的位置. 【输入形式】控制台分行输入字符串s,t. 【输出形式】控制台输出一个整数,是t在s最右边出现的位置. 【样例输入】The strdup() function new returns a pointer to a new string new 【样例输出】49 【样例说明】输入的第一行为字符串s,第二行为字符串t="new".t在s中出现过两次,其中在最右边出现的位置中"new"的第一个字符“n”;在s中所在的位置为49. def strrindex(s,t): pos = 0 pos1 = -1 while True: pos = s.find(t,pos) if pos == -1: else: pos1 = pos pos = pos + len(t) if __name__ == "__main__": s=input() t=input() print(strrindex(s,t))
时间: 2023-11-22 08:55:34 浏览: 117
这道题的思路是利用字符串函数find()从左往右查找t在s中出现的位置,然后用pos1记录下最后一次出现的位置。代码如下:
```python
def strrindex(s,t):
pos = 0
pos1 = -1
while True:
pos = s.find(t, pos)
if pos == -1:
break
else:
pos1 = pos
pos = pos + len(t)
return pos1
if __name__ == "__main__":
s = input()
t = input()
print(strrindex(s,t))
```
输入样例:
```
The strdup() function new returns a pointer to a new string new
new
```
输出样例:
```
49
```
相关问题
[问题描述]补充完整函数strrindex(s,t),其功能用于返回字符串t在字符串s中最右边出现的位置,该位置从0开始计数,如果s中不含有t,那么返回-1;在你编写的程序中,使用strrindex(s,t)函数输入t,s,输出t在s最右边的位置
下面是完整的函数实现:
```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。
补充完整函数strrindex(s,t),其功能用于返回字符串t在字符串s中最右边出现的位置.该位置从0开始计数,如果s中不含有t,那么返回-1;在你编写的程序中,使用strrindex(s,t)函数,输入t,s,输出t在s最右边的位置.
### 回答1:
strrindex(s,t)函数功能是用于返回字符串t在字符串s中最右边出现的位置。该位置从0开始计数,如果s中不包含t,则返回-1。在你编写的程序中,使用strrindex(s,t)函数,输入t、s,输出t在s最右边出现的位置。
### 回答2:
补充完整函数strrindex(s,t),其功能用于返回字符串t在字符串s中最右边出现的位置。该位置从0开始计数,如果s中不含有t,那么返回-1。
下面是函数strrindex(s,t)的完整代码实现:
```python
def strrindex(s, t):
length_s = len(s)
length_t = len(t)
# 如果t的长度大于s的长度,则不可能在s中出现
if length_t > length_s:
return -1
# 从s的右边开始搜索,每次向左移动一位
for i in range(length_s-1, length_t-2, -1):
flag = True
# 遍历比较t与s中对应位置的字符
for j in range(length_t):
if s[i-j] != t[length_t-1-j]:
flag = False
break
# 如果t与s中相应位置的字符都相等,则找到了t在s中的位置
if flag:
return i - length_t + 1
# 如果s中不含有t
return -1
```
使用strrindex(s,t)函数来查找t在s中最右边的位置的示例代码如下:
```python
s = "hello world"
t = "or"
index = strrindex(s, t)
print(index)
```
输出结果为:
```
7
```
表示字符"or"在字符串"s"中最右边出现的位置是7。
### 回答3:
以下是strrindex(s,t)函数的编写过程:
```python
def strrindex(s, t):
# 判断t是否为空字符串
if t == "":
return -1
# 定义结果变量,初始为-1
result = -1
# 从字符串s的末尾开始遍历
for i in range(len(s)-1, -1, -1):
# 判断从当前位置开始的子字符串是否与t相同
if s[i:i + len(t)] == t:
# 更新结果变量为当前位置
result = i
# 结束循环
break
# 返回最右边出现的位置
return result
```
调用该函数进行测试:
```python
s = "Hello, world!"
t = "o"
position = strrindex(s, t)
print(position)
```
在以上示例中,字符串s为"Hello, world!",字符串t为"o"(要注意字符串t只包含一个字符)。运行结果为 8,表示字符"o"在字符串s中最右边出现的位置是从0开始计数的第8位。
如果字符串s不包含字符串t,即调用`strrindex(s, t)`时返回-1。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)