#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");
}
我需要帮助实现一个函数,该函数将破译字符串中的字符是否包含字母表中的所有字母,以及它们是否允许用户知道哪些字母缺失。
答案 0 :(得分:1)
你是否接受过关于不变量的教导?我建议你寻找一个概括这两个特殊情况的不变量:
如果您没有查看句子的任何部分,您必须考虑所有字母都缺失。
如果您查看了所有句子,那么正如您所写,missing
数据结构恰好包含句子中缺少的那些字母。
我还建议您查找ANSI C函数isalpha
和tolower
。
答案 1 :(得分:1)
没有做作业的答案......
1) get the character
2) is it a "."
a) check if you have all and return result.
b) continue
3) is it greater than/equal "A" but less than/equal "Z" ( this defines a range of characters )
a) add it to list return to (1)
b) continue
4) is it greater than/equal "a" but less than/equal "z" ( another range, could be combined with the first )
a) add it to list return to (1)
b) continue
5) is it a " " ( a space .. but could be another range, could be combined with the first )
a) continue
6) error not a correct character and exit
答案 2 :(得分:0)
在check
内,您可以遍历字符串sentence
,对于您遇到的每个字符,请将该字符的missing
更改为1
。
最后,丢失的字符将标记为0.如果missing
至少包含0
,则返回0
,否则1
。
我不打算编写完整的代码,但有一些提示可以帮助您入门:
1)您可以使用currentCharacter - 'a'
标记正确的元素。这将返回字符currentCharacter
的索引。
2)您可以使用
遍历字符串char currentCharacter;
while( currentCharacter = *(sentence++) )
{
//mark array here
}
答案 3 :(得分:0)
只需遍历输入字符串,标记其中包含哪些字符,然后检查缺少的字符:
int main()
{
char str[] = "Some meaningful text";
int freq[256];
int i;
for ( i = 0; i < 256; i ++) // clear frequency array
{
freq[i] = 0;
}
for (i = 0; str[i] != '\0'; i++) // parse input string
{
freq[str[i]]++;
}
for ( i = 0; i < 256; i ++)
{
if (freq[i]==0 && isalpha(i)) // find out which leters weren't typed
{
printf("%c letter wasn't typed!\n", (char)i);
}
}
return 0;
}
答案 4 :(得分:0)
我能想到的最简单的方法就是遍历字母表中的每个字母并检查字母是否在句子中(通过循环查看字母,直到找到字母或者到达字母的末尾)句子)。如果字母不在句子中,请将数字(对应于缺失的字母)添加到数组中。
这是工作职能:
int check(char *sentence, int missing[26])
{
int missIndex = 0; //the index for the missing array;
bool iWasFound; //was the letter found? (used in the loops below)
for(char i = 'A'; i <= 'Z'; i++)
{
iWasFound = false;
for(int j = 0; j < 1024; j++)
{
if(toupper(sentence[j]) == i)
{
iWasFound = true;
break;
}
}
//if we did not find the letter, add the corresponding number to the missing array
if(!iWasFound)
{
missing[missIndex] = (int)(i - 'A');
cout << (int)(i - 'A') << " | " << missing[missIndex] << std::endl;
missIndex++;
}
}
if(missing[0] == -1)
{
return 1;
}
else
{
return 0;
}
}
如果您需要我解释任何事情,请告诉我