0
#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");
}

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

4

5 回答 5

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
于 2012-05-27T22:16:05.073 回答
1

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

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

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

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

于 2012-05-27T20:14:31.157 回答
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;
  }
}

如果您需要我解释任何事情,请告诉我

于 2012-11-21T02:38:34.177 回答
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;
}
于 2012-05-27T20:30:59.723 回答
0

在内部check,您可以遍历字符串sentence,并为遇到的每个字符更改missing为该1字符。

最后,缺少的字符将被标记为 0。如果missing至少包含一个0,则返回0,否则1

我不打算编写完整的代码,但一些提示可以帮助您入门:

1) 您可以使用 标记正确的元素currentCharacter - 'a'。这将为您返回 character 的索引currentCharacter

2)您可以使用遍历字符串

char currentCharacter;
while( currentCharacter = *(sentence++) )
{
   //mark array here
}
于 2012-05-27T20:13:57.243 回答