2016-07-31 20 views
-1

程序提示用户键入存储在两个不同数组中的两个单词。 如果单词是anagram,它会打印“Anagram”,如果不是则打印“Not Anagram”。我为所有的字母组成了一个数组,字母'a'被存储为{1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...},填满了整个字母组合。C程序来查找两个单词是否是anagrams(我的代码在里面)

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

#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; 
} 

我很感激任何帮助,我可以得到。

回答

1
bool equal_array(int counts1[26],int counts2[26]) 
... 
for(i=0; i<30; i++) 

意味着你最终会通过数组元素月30日只有26个元素比较第27位。这是(1)未定义的行为,(2)明显的错误,以及(3)如果它没有崩溃和燃烧,可能会产生错误的结果。


我不明白为什么你的读取循环中有30个字符的限制。你不会在任何地方储存这些词,所以没有理由任意限制它们的长度。另一方面,您不检查字母是否在A .. Z范围内,所以如果用户输入非字母字符,您的函数将修改count数组范围外的一些随机字节,从而导致到未定义的行为,如上所述。


最后,这将是一个短一点写:

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; 
} 

有些人不喜欢在这样的循环的中间收益,因此,如果你的教授是其中的一个人我假设你需要布尔变量。但是我个人觉得上面的版本更易于阅读。

0
bool equal_array(int counts1[26],int counts2[26]) 
{ 
    int i; 
    bool is_anagram=true; 
    for(i=0; i<26; i++)//30 : occures out of bounds 
    { 
     if(counts1[i] != counts2[i]) 
     { 
      is_anagram=false; 
      break; 
     } 
    } 
    return is_anagram; 
} 
0
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; 
    } 
} 
相关问题