1

我之前得到了一些帮助来修复我在这个程序中使用的一个功能,但现在我失去了逻辑。

我在这个程序中有三个目的和两个功能。第一个目的是打印用户向后输入的句子。第二个目的是检查句子中是否有任何单词与另一个单词是字谜。第三个目的是检查任何一个单词是否是回文。

我成功地完成了第一个目的。我可以倒着打印句子。但是现在我不确定我应该如何实现我的函数来检查任何单词是否是字谜或回文。

这是代码;

/*
 * Ch8pp14.c
 *
 *  Created on: Oct 12, 2013
 *      Author: RivalDog
 *      Purpose: Reverse a sentence, check for anagrams and palindromes
 */

#include <stdio.h>
#include <ctype.h> //Included ctype for tolower / toupper functions
#define bool int
#define true 1
#define false 0

//Write boolean function that will check if a word is an anagram
bool check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;
// Convert arrays into all lower case letters
   while(a[c])
   {
       a[c] = (tolower(a[c]));
       c++;
   }

   c = 0;

   while(b[c])
      {
       b[c] = (tolower(b[c]));
       c++;
      }

      c = 0;

   while (a[c] != 0)
   {
      first[a[c]-'a']++;
      c++;
   }

   c = 0;

   while (b[c] != 0)
   {
      second[b[c]-'a']++;
      c++;
   }

   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return false;
   }

   return true;
}

//Write boolean function that will check if a word is a palindrome
bool palindrome(char a[])
{
    int c=0, j, k;
    //Convert array into all lower case letters
    while (a[c])
    {
        a[c] = (tolower(a[c]));
        c++;
    }

    c = 0;
    j = 0;
    k = strlen(a) - 1;
    while (j < k)
    {
        if(a[j++] != a[k--])
            return false;
    }

    return true;
}

int main(void)
{
    int i = 0, j = 0, k = 0;
    char a[80], terminator;
    //Prompt user to enter sentence, store it into an array
    printf("Enter a sentence: ");
    j = getchar();
    while (i < 80)
    {
        a[i] = j;
        ++i;
        j = getchar();
        if (j == '!' || j == '.' || j == '?')
        {
            terminator = j;
            break;
        }
        else if(j == '\n')
        {
            break;
        }
    }
    while(a[k])
    {
        a[k] = (tolower(a[k]));
        k++;
    }
    k = 0;
    while(k < i)
    {
        printf("%c", a[k]);
        k++;
    }
    printf("%c\n", terminator);
    //Search backwards through the loop for the start of the last word
    //print the word, and then repeat that process for the rest of the words
    for(j = i; j >= 0; j--)
    {
        while(j > -1)
        {
            if (j == 0)
            {
                for(k=j;k<i;k++)
                    {
                        printf("%c", a[k]);
                    }
                printf("%c", terminator);
                    break;
            }
            else if (a[j] != ' ')
                --j;
            else if (a[j] == ' ')
                {
                    for(k=j+1;k<i;k++)
                        {
                            printf("%c", a[k]);
                        }
                    printf(" ");
                        break;
                }
        }
        i = j;
    }
    //Check if the words are anagrams using previously written function
    for( i = 0; i < 80; i++)
    {
        if (a[i] == ' ')
        {

        }
    }

    //Check if the words are palindromes using previously written function

return 0;
}

我在想,也许我可以通过检查元素是否是空格来再次在数组中搜索单词,如果是,则将搜索开始的位置存储到新数组中的空间索引 1,重复该过程整个句子,然后在所有数组上调用我的函数。我看到的问题是我无法真正预测用户将在一个句子中输入多少个单词......那么我如何将我的代码设置为可以检查字谜/回文的位置?

谢谢大家!

~RivalDog

4

2 回答 2

0

如果您首先优化代码并通过添加注释使其可读性会更好。然后您可以将问题分成更小的部分,例如 1.如何计算字符串中的单词?2.如何判断两个单词是否为字谜?3.如何判断一个单词是否为回文?您可以通过谷歌搜索轻松获得这些较小的程序。那么你的工作就是整合这些答案。希望这可以帮助。

于 2013-10-13T19:43:07.343 回答
0

要检查字谜,无需计算单词的数量并一一比较它们或任何你在想的东西。
看看这段代码。在此代码中,函数read_word()使用 26 个元素的数组读取单词/短语输入int,以跟踪每个字母被看到的次数,而不是存储字母本身。另一个功能equal_array()是检查 arrayab(in main) 是否相等 (anagram) 并返回一个布尔值作为结果。

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

void read_word(int counts[26]);
bool equal_array(int counts1[26],int counts2[26]);

int main()
{
    int a[26] = {0}, b[26] = {0};

    printf("Enter first word/phrase: ");
    read_word(a);
    printf("Enter second word/phrase: ");
    read_word(b);

    bool flag = equal_array(a,b);
    printf("The words/phrase are ");
    if(flag)
        printf("anagrams");
    else
        printf("not anagrams"); 

    return 0;
}

void read_word(int counts[26])
{
    int ch;
    while((ch = getchar()) != '\n')
    if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
        counts[toupper(ch) - 'A']++;
}

bool equal_array(int counts1[26],int counts2[26])
{
    int i = 0;
    while(i < 26)
    {
        if(counts1[i] == counts2[i])
            i++;
        else
            break;  
    }

    return i == 26 ? true : false;  
}
于 2013-10-14T01:25:58.210 回答