70分求调!!!
查看原帖
70分求调!!!
1474720
codingRE楼主2024/11/20 21:59
#include <string.h>

char pass[100050];

int main() {
    char word1[10] = "\0";
    fgets(word1, sizeof(word1), stdin);
    fgets(pass, sizeof(pass), stdin);

    // 去掉末尾的换行符
    word1[strcspn(word1, "\n")] = '\0';
    pass[strcspn(pass, "\n")] = '\0';

    int len = strlen(pass);
    int len2 = strlen(word1);
    for (int i = 0; i < len2; ++i) {
        if (word1[i] >= 'a' && word1[i] <= 'z') {
            word1[i] -= 32;
        }
    }
    for (int i = 0; i < len; ++i) {
        if (pass[i] >= 'a' && pass[i] <= 'z') {
            pass[i] -= 32;
        }
    }

    int count = 0;
    int first_pos = -1;
    char* p1 = pass;

    while (1) {
        char* space = strchr(p1, ' ');
        if (space) {
            *space = '\0'; // 在空格处截断字符串
        }

        // 比较当前单词
        if (strcmp(p1, word1) == 0) {
            count++;
            if (first_pos == -1) {
                first_pos = p1 - pass;
            }
        }

        if (space) {
            *space = ' '; // 恢复空格
            p1 = space + 1; // 移动到下一个单词
            while (p1 == ' ')
                p1++;
        }
        else {
            break; // 没有更多单词
        }
    }

    if (count == 0) {
        printf("-1\n");
    }
    else {
        printf("%d %d\n", count, first_pos);
    }

    return 0;
}
2024/11/20 21:59
加载中...