帮我用简单的c语言串写一个查找字符串中所有指定的字符的程序
时间: 2024-05-14 12:17:34 浏览: 16
好的,下面是一个简单的 C 语言程序,用于查找字符串中所有指定的字符:
```
#include <stdio.h>
#include <string.h>
void find_char(char* str, char c) {
int len = strlen(str);
int count = 0;
for (int i = 0; i < len; i++) {
if (str[i] == c) {
printf("%c found at index %d\n", c, i);
count++;
}
}
if (count == 0) {
printf("%c not found in the string\n", c);
}
}
int main() {
char str[100];
char c;
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("Enter the character to find: ");
scanf("%c", &c);
find_char(str, c);
return 0;
}
```
在这个程序中,我们定义了一个 `find_char()` 函数,用于查找字符串中所有指定的字符。该函数有两个参数:字符串和字符。它遍历字符串中的每个字符,并检查是否与指定字符相同。如果相同,它将打印出该字符在字符串中的索引,并增加计数器。如果没有找到任何字符,则打印一个消息。
在 `main()` 函数中,我们首先从用户输入中读取字符串和字符,然后调用 `find_char()` 函数来查找字符串中所有指定的字符。
希望这个程序能够帮助到你!
相关推荐
![](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)
![](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)