你写的不太对请你重新写一遍正确的
时间: 2024-03-13 18:43:09 浏览: 21
非常抱歉,我之前的回答有误。以下是符合题目要求的 C 代码:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char ch; // 待查找的字符
char str[81]; // 字符串
int i, index = -1; // index 初值为 -1,表示未找到字符
scanf("%c", &ch);
getchar(); // 消耗掉回车符
fgets(str, 81, stdin);
str[strlen(str) - 1] = '\0'; // 将字符串最后的回车符替换为字符串结束符
for (i = 0; i < strlen(str); i++)
{
if (str[i] == ch)
{
index = i;
}
}
if (index == -1)
{
printf("Not Found");
}
else
{
printf("index = %d", index);
}
return 0;
}
```
程序运行时,首先从标准输入中读入待查找的字符,然后再读入字符串。接下来遍历字符串,查找该字符在字符串中最后一次出现的位置,如果找到则更新 `index` 的值。最后根据 `index` 的值输出结果。
需要特别注意的是,由于 `fgets` 会读入字符串末尾的回车符,因此需要将其替换为字符串结束符。