求大佬指导,都不对c语言
查看原帖
求大佬指导,都不对c语言
1290501
hgdhjgj楼主2024/4/16 23:55
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char str[100], sent[100000];
    int pos, counts = 0, ans;

    fgets(str, sizeof(str), stdin);
    fgets(sent, sizeof(sent), stdin);

    // 首先进行大小写转换
    for (int i = 0; i < strlen(str); i++) {
        if (isupper(str[i])) {
            str[i] = tolower(str[i]);
        }
    }

    for (int i = 0; i < strlen(sent); i++) {
        if (isupper(sent[i])) {
            sent[i] = tolower(sent[i]);
        }
    }

    strcat(str, " ");
    strcat(sent, " ");

    // 完全匹配
    pos = strstr(sent, str) - sent;
    ans = pos;
    while (pos != -1) {
        counts++;
        pos = strstr(sent + pos + 1, str) - sent;
    }

    if (ans == -1) {
        printf("-1");
    } else {
        printf("%d %d", counts, ans);
    }

    return 0;
}
2024/4/16 23:55
加载中...