编写一子函数查找指定字符在字符串中第一次和最后一次出现的位置
时间: 2024-03-10 07:51:06 浏览: 17
好的,下面是代码:
```python
def find_first_and_last(s, c):
first = s.find(c)
last = s.rfind(c)
return first, last
```
这个函数接受两个参数,一个是字符串 `s`,另一个是要查找的字符 `c`。它会使用字符串的 `find` 方法来查找第一次出现的位置,使用 `rfind` 方法来查找最后一次出现的位置。最后,它会返回一个包含两个值的元组,第一个值是第一次出现的位置,第二个值是最后一次出现的位置。
相关问题
用c编写程序查找指定字符在字符串中第一次和最后一次出现的位置,要用到子函数
好的,下面是使用 C 语言编写的程序,它可以查找指定字符在字符串中第一次和最后一次出现的位置,并使用子函数实现。
```c
#include <stdio.h>
#include <string.h>
void find_first_and_last(char *s, char c, int *first, int *last) {
*first = strchr(s, c) - s;
*last = strrchr(s, c) - s;
}
int main() {
char s[100], c;
int first, last;
printf("Enter a string: ");
fgets(s, 100, stdin);
printf("Enter a character to find: ");
scanf("%c", &c);
find_first_and_last(s, c, &first, &last);
if (first == -1) {
printf("The character '%c' is not found in the string.\n", c);
} else {
printf("The first occurrence of '%c' is at position %d.\n", c, first);
printf("The last occurrence of '%c' is at position %d.\n", c, last);
}
return 0;
}
```
这个程序首先定义了一个 `find_first_and_last` 函数,它接受三个参数:一个指向字符串的指针 `s`,要查找的字符 `c`,以及两个指向整数的指针 `first` 和 `last`。这个函数使用了 C 语言标准库中的 `strchr` 和 `strrchr` 函数来分别查找第一次和最后一次出现的位置,并计算出它们在字符串中的索引。最后,它通过修改指针变量 `first` 和 `last` 的值来返回这两个位置。
在 `main` 函数中,我们首先读入一个字符串和要查找的字符,并调用 `find_first_and_last` 函数来查找这个字符在字符串中第一次和最后一次出现的位置。如果这个字符没有在字符串中出现过,那么 `find_first_and_last` 函数会把 `first` 和 `last` 的值都设置为 -1。在 `main` 函数中,我们通过检查 `first` 是否等于 -1 来判断这个字符是否在字符串中出现过。如果没有出现过,我们就输出一条错误信息。否则,我们输出这个字符在字符串中第一次和最后一次出现的位置。
C语言中,编写函数,判断一个字符串是否包含另一个字符串,并在主函数中测试结果
可以使用C语言中的strstr函数来判断一个字符串是否包含另一个字符串。该函数的原型为:
```c
char *strstr(const char *str1, const char *str2);
```
其中,str1表示要查找的字符串,str2表示要查找的子字符串。如果str2是str1的子串,则返回str2在str1中第一次出现的位置的指针;如果str2不是str1的子串,则返回NULL。
下面是一个示例代码,可以判断一个字符串是否包含另一个字符串。
```c
#include <stdio.h>
#include <string.h>
int isContain(char *str1, char *str2) {
if (strstr(str1, str2) != NULL) {
return 1;
} else {
return 0;
}
}
int main() {
char str1[100], str2[100];
printf("请输入字符串1:");
scanf("%s", str1);
printf("请输入字符串2:");
scanf("%s", str2);
if (isContain(str1, str2)) {
printf("字符串1包含字符串2\n");
} else {
printf("字符串1不包含字符串2\n");
}
return 0;
}
```
在上述代码中,isContain函数用来判断str1是否包含str2;主函数中输入两个字符串,并调用isContain函数进行判断。运行程序后,会输出相应的判断结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)