gpt4 book ai didi

c - 如何检查字母表中的所有字母是否都在带有 C 的数组中(检查它是否是全字母表)

转载 作者:行者123 更新时间:2023-11-30 14:25:04 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <ctype.h>

/* prototypes for functions */
void getstring(char *sentence);
int check(char *sentence, int missing[26]);
void showNegativeResults(int[]);

int main(void) {
char sentence[1024] = {'\0'};
int missing[26] = {0};

printf("Enter sentence\n(ending with a period like this one).\n\n");
getstring(sentence);

printf("\nSentence: \"%s.\"", sentence);

if ( check(sentence, missing) )
printf("\n\nThe sentence IS a pangram!\n\n");
else
showNegativeResults(missing);

return 0;
}

void getstring(char *sentence) {
int j = 0;
while ((sentence[j] = getchar()) != '.')
j++;
sentence[j] = '\0';
}

int check(char *sentence, int missing[26]) {

return 1; /* return a 1 if it is a pangram*/

return 0; /*return 0 if it is not a pangram */
}

void showNegativeResults(int missing[26]) {
int c;
printf("\n\nThe sentence is NOT a pangram.\n");
printf("Missing letters:");
for(c = 0; c < 26; c++)
if (missing[c])
printf(" %c", ('a' + c));
printf("\n\n");
}

我需要帮助实现一个函数,该函数将破译字符串中的字符是否包含字母表中的所有字母,以及它们是否不允许用户知道缺少哪些字母。

最佳答案

您学过不变量吗?我建议您寻找一个概括这两种特殊情况的不变量:

  • 如果您没有查看句子的任何部分,则必须认为所有字母都丢失了。

  • 如果您已经查看了整个句子,那么正如您所写,missing 数据结构恰好包含句子中缺少的那些字母。

    <

我还建议您查找 ANSI C 函数 isalphatolower

关于c - 如何检查字母表中的所有字母是否都在带有 C 的数组中(检查它是否是全字母表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10776890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com