C程序查找两个单词是否为字谜(我的代码在里面)

C程序查找两个单词是否为字谜(我的代码在里面),c,arrays,function,C,Arrays,Function,程序提示用户键入存储在两个不同数组中的两个单词。 如果单词是字谜,它会打印“字谜”,如果不是,它会打印“非字谜”。我为所有的字母做了一个数组,字母“a”存储为{1 0 0 0 0 0 0 0 0 0 0…}填充整个字母数组 然后我比较了两个数组,以确定它们是否是相同的单词,如果它们是0(相互抵消),则它们是字谜。 这是到目前为止我的代码,我不确定我做错了什么。我很确定布尔函数有问题 #include <stdio.h> #include <stdbool.h> #incl

程序提示用户键入存储在两个不同数组中的两个单词。 如果单词是字谜,它会打印“字谜”,如果不是,它会打印“非字谜”。我为所有的字母做了一个数组,字母“a”存储为{1 0 0 0 0 0 0 0 0 0 0…}填充整个字母数组

然后我比较了两个数组,以确定它们是否是相同的单词,如果它们是0(相互抵消),则它们是字谜。 这是到目前为止我的代码,我不确定我做错了什么。我很确定布尔函数有问题

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

void read_word(int counts[26])
{
    int i;
    char ch;

    printf("Enter a word: ");
    for(i=0;(ch=getchar()) != '\n' && i<30; i++)
        counts[toupper(ch)-'A']++;
}

bool equal_array(int counts1[26],int counts2[26])
{
    int i;
    bool is_anagram=false;   
    for(i=0; i<30; i++)
    {
        counts1[i]= counts1[i] - counts2[i];
        if(counts1[i] == 0)
            {
              is_anagram=true;
            }
        else
        {
            is_anagram=false;
            break;
        }

    }
   return is_anagram;
}

int main()
{

    int first_word[26]={0};
    int second_word[26]={0};


    read_word(first_word);

    read_word(second_word);


     if( equal_array(first_word,second_word) == true)
        printf("Anagram");
    else
        printf("Not Anagram");

    return 0;
}
#包括
#包括
#包括
无效读取字(整数计数[26])
{
int i;
char ch;
printf(“输入一个单词:”);
对于(i=0;(ch=getchar())!='\n'和&i
bool-equal_数组(int-counts1[26],int-counts2[26])
{
int i;
bool是_anagram=true;
对于(i=0;i
有些人不喜欢这样的循环中的返回,所以如果你的教授是这些人中的一个,我想你需要布尔变量。但是我个人觉得上面的版本更容易阅读。
class Program
{
    public static void Main(string[] args)
    {
        string firstWord = String.Empty;
        string secondWord = String.Empty;
        Console.WriteLine("Check if two strings are anagrams");
        Console.WriteLine("Enter First String");
        firstWord = Console.ReadLine();
        Console.WriteLine("Enter Second String");
        secondWord = Console.ReadLine();
        Console.WriteLine();
        Console.WriteLine("Ara Anagram: " + AreAnagrams(firstWord.ToLower(), secondWord.ToLower()).ToString());
        Console.ReadLine();
    }

    private static bool AreAnagrams(string firstWord, string secondWord)
    {
        if (firstWord.Length == 0 || firstWord.Length != secondWord.Length)
            return false;

        string letters = new String(firstWord.Distinct().ToArray());

        foreach (char letter in letters)
        {
            char lowerLetter = Char.ToLower(letter);
            if (firstWord.Count(c => c == lowerLetter) != secondWord.Count(c => c == lowerLetter)) return false;
        }
        return true;
    }
}
bool equal_array(int counts1[26],int counts2[26])
...
for(i=0; i<30; i++)
bool equal_array(int counts1[26],int counts2[26]) {
    int i;
    for(i=0; i<26; i++) {
        if(counts1[i] != counts2[i]) return false;
    }
    return true;
}
class Program
{
    public static void Main(string[] args)
    {
        string firstWord = String.Empty;
        string secondWord = String.Empty;
        Console.WriteLine("Check if two strings are anagrams");
        Console.WriteLine("Enter First String");
        firstWord = Console.ReadLine();
        Console.WriteLine("Enter Second String");
        secondWord = Console.ReadLine();
        Console.WriteLine();
        Console.WriteLine("Ara Anagram: " + AreAnagrams(firstWord.ToLower(), secondWord.ToLower()).ToString());
        Console.ReadLine();
    }

    private static bool AreAnagrams(string firstWord, string secondWord)
    {
        if (firstWord.Length == 0 || firstWord.Length != secondWord.Length)
            return false;

        string letters = new String(firstWord.Distinct().ToArray());

        foreach (char letter in letters)
        {
            char lowerLetter = Char.ToLower(letter);
            if (firstWord.Count(c => c == lowerLetter) != secondWord.Count(c => c == lowerLetter)) return false;
        }
        return true;
    }
}