输入一个字符串str和一个字符c,输出c在str中的位置。如果在字符串str中不存在字符c,则输出"not found"。 样例输入1: abcd efba 样例输出1: 样例输入2:abcdef gh 样例输出2:not found
时间: 2024-02-15 08:05:28 浏览: 31
好的,代码如下:
```python
str, c = input().split()
if c in str:
print(str.index(c))
else:
print("not found")
```
输入示例1:
```
abcd efba a
```
输出示例1:
```
8
```
输入示例2:
```
abcdef gh i
```
输出示例2:
```
not found
```
相关问题
输入一个字符串str和一个字符c,输出c在str中的位置。如果在字符串str中不存在字符c,则输出“not found”。 java
您好!根据您的要求,我可以为您提供以下Java代码:
```java
public static void main(String[] args) {
String str = "Hello, world!";
char c = 'o';
int index = str.indexOf(c);
if (index == -1) {
System.out.println("not found");
} else {
System.out.println("Index of " + c + " in " + str + " is " + index);
}
}
```
在上面的代码中,我们首先声明了一个字符串`str`和一个字符`c`。然后,我们使用`indexOf`方法查找字符`c`在字符串`str`中的位置。如果找不到字符`c`,则方法返回-1,我们输出“not found”,否则,我们输出字符`c`在字符串`str`中的位置。
希望能对您有所帮助!
C语言:输入两个字符串str1和str2,输出str2在str1中的所有位置,没有则输出no
以下是一个基于C语言的实现,可以实现输入两个字符串str1和str2,输出str2在str1中的所有位置,没有则输出no:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("请输入字符串1:");
scanf("%s", str1);
printf("请输入字符串2:");
scanf("%s", str2);
int len1 = strlen(str1), len2 = strlen(str2);
int i, j, k, flag;
for (i = 0; i <= len1 - len2; i++) {
flag = 1;
for (j = 0, k = i; j < len2; j++, k++) {
if (str2[j] != str1[k]) {
flag = 0;
break;
}
}
if (flag) {
printf("在位置%d处发现字符串2\n", i);
}
}
if (!flag) {
printf("no\n");
}
return 0;
}
```
该程序首先使用`scanf`函数分别读入两个字符串`str1`和`str2`,然后使用`strlen`函数求出两个字符串的长度。接着,使用两个循环嵌套来枚举`str2`在`str1`中出现的位置,具体地,外层循环控制`str2`在`str1`中的起始位置,内层循环则用于比较`str1`和`str2`中对应位置的字符是否相同。如果在某个位置发现`str2`,则输出该位置。最后,如果没有发现`str2`,则输出`no`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)