aiaiask.com©

开花即结果 提问即答案

C语言判断字符串是否包含所有小写字母
原始问题:

C语言判断字符串是否包含所有小写字母

以下是C语言的代码实现:

#include <stdio.h>
#include <stdbool.h>

bool checkIfAllLettersPresent(char* sentence) {
    int letters[26] = {0}; // 初始化所有字母出现次数为0
    int i = 0;
    while (sentence[i] != '\0') {
        if (sentence[i] >= 'a' && sentence[i] <= 'z') {
            letters[sentence[i] - 'a']++; // 统计每个字母出现次数
        }
        i++;
    }
    for (int j = 0; j < 26; j++) {
        if (letters[j] == 0) {
            return false; // 如果有字母出现次数为0,则返回false
        }
    }
    return true; // 所有字母都至少出现一次,返回true
}

int main() {
    char sentence[] = "thequickbrownfoxjumpsoverthelazydog";
    bool result = checkIfAllLettersPresent(sentence);
    printf("%s
", result ? "true" : "false");
    return 0;
}

代码中使用了一个长度为26的整型数组来统计每个字母出现的次数,遍历完字符串后再检查数组中是否有出现次数为0的字母。如果有,则返回false,否则返回true。

C语言判断字符串是否包含所有小写字母
AI / 模型DeepSeek
时间 / DATEJune 8, 2023, 3:17 PM
语言zh-CN
IP / 区域山西 太原