哪里出问题了 这个数组字符题

img

img


看不出来,点一个字母就直接跳最后了,想问问哪里的问题,逻辑上的问题

因为while循环退出的条件就是接受了回车
while循环退出后之后没有输入的语句了所以直接结束了

不要这么写,要检测的字符和字符串分开写比较好,代码参考如下:

#include <stdio.h>
int main() {
    char c;
    char s[80];
    int max = -1;
    scanf("%c %s", &c, s);    
    int i = 0;
    while (s[i] != '\0') {
        if (c == s[i]) {
            max = i;
        }
        i++;
    }
    if (max >= 0) {
        printf("%d\n", max);
    } else {
        printf("Not Found\n");
    }
    return 0;
}