帮我用c语言写一个简单的查找指定字符的程序
时间: 2023-12-05 19:04:08 浏览: 28
好的,以下是一个简单的查找指定字符的程序:
```c
#include <stdio.h>
int main() {
char str[100];
char ch;
int i, count = 0;
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin); // 从键盘读入字符串
printf("请输入要查找的字符:");
scanf("%c", &ch); // 从键盘读入要查找的字符
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++;
}
}
printf("字符 %c 在字符串中出现了 %d 次。\n", ch, count);
return 0;
}
```
程序的功能是从键盘读入一个字符串和一个字符,然后统计该字符在字符串中出现的次数并输出。
相关问题
帮我用简单的c语言串写一个查找字符串中所有指定的字符的程序
好的,下面是一个简单的 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()` 函数来查找字符串中所有指定的字符。
希望这个程序能够帮助到你!
编写一个c语言查找指定字符串程序
当然可以,以下是一个简单的例子:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("请输入一个字符串: ");
gets(str1);
printf("请输入想要查找的字符串: ");
gets(str2);
if (strstr(str1, str2) != NULL)
printf("查找成功!\n");
else
printf("查找失败!\n");
return 0;
}