首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何实现我的构词和回文函数来检查用户输入的单词?

我如何实现我的构词和回文函数来检查用户输入的单词?
EN

Stack Overflow用户
提问于 2013-10-14 01:40:08
回答 2查看 1.5K关注 0票数 1

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

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

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

这是代码;

代码语言:javascript
复制
/*
 * 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

EN

Stack Overflow用户

发布于 2013-10-14 09:25:58

要检查字形,不需要计算单词的数量,并逐个比较它们或任何你想的东西。

看看这段代码。在这个代码函数中,read_word()使用一个包含26个元素的int数组读取单词/短语输入,以跟踪每个字母被看到的次数,而不是存储字母本身。另一个函数equal_array()是检查数组ab (在main中)是否相等(anagram),并返回一个布尔值作为结果。

代码语言:javascript
复制
#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;  
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19348222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档