F
Felipe Ribeiro
Hello everyone!
So, I was trying to write a program that checks whether two words are
anagrams. I wrote the code but for some reason I can't figure out, the
program won't behave as it should.
Here goes the code:
-----------------------------------------------------------------------------------------
#include <stdio.h>
#include <ctype.h>
#define LEN 26
#define TRUE 1
#define FALSE 0
typedef int bool;
void read_word(int letters_in_word[], int array_length);
bool are_anagrams(int letters_in_word1[], int letters_in_word2[],
int array_length);
int main(void)
{
/* The number of times each letter appears in the words */
int letters_word1[LEN] = {0}, letters_word2[LEN] = {0}, i;
printf("Enter first word: ");
read_word(letters_word1, LEN);
printf("Enter second word: ");
read_word(letters_word2, LEN);
if (are_anagrams(letters_word1, letters_word2, LEN))
printf("The words are anagrams.\n");
else
printf("The words are not anagrams.\n");
return 0;
}
void read_word(int word[], int length)
{
char ch;
while ((ch = getchar() != '\n')) {
ch = toupper(ch);
word[ch - 'A']++;
}
}
/*
* If the number of times each letter appears in each word is the
same,
* then the words are anagrams.
*/
bool are_anagrams(int word1[], int word2[], int length)
{
int i;
for (i = 0; i < length; i++)
if (word1 != word2)
return FALSE;
return TRUE;
}
-----------------------------------------------------------------------------------------
It might be some obvious mistake; I'm just a beginner.
I've been looking at it for some time and can't find out what the
error is. It seems to me that are_anagrams() always returns TRUE and I
can't understand why.
I'd appreciate any help you could give me.
Thanks in advance.
So, I was trying to write a program that checks whether two words are
anagrams. I wrote the code but for some reason I can't figure out, the
program won't behave as it should.
Here goes the code:
-----------------------------------------------------------------------------------------
#include <stdio.h>
#include <ctype.h>
#define LEN 26
#define TRUE 1
#define FALSE 0
typedef int bool;
void read_word(int letters_in_word[], int array_length);
bool are_anagrams(int letters_in_word1[], int letters_in_word2[],
int array_length);
int main(void)
{
/* The number of times each letter appears in the words */
int letters_word1[LEN] = {0}, letters_word2[LEN] = {0}, i;
printf("Enter first word: ");
read_word(letters_word1, LEN);
printf("Enter second word: ");
read_word(letters_word2, LEN);
if (are_anagrams(letters_word1, letters_word2, LEN))
printf("The words are anagrams.\n");
else
printf("The words are not anagrams.\n");
return 0;
}
void read_word(int word[], int length)
{
char ch;
while ((ch = getchar() != '\n')) {
ch = toupper(ch);
word[ch - 'A']++;
}
}
/*
* If the number of times each letter appears in each word is the
same,
* then the words are anagrams.
*/
bool are_anagrams(int word1[], int word2[], int length)
{
int i;
for (i = 0; i < length; i++)
if (word1 != word2)
return FALSE;
return TRUE;
}
-----------------------------------------------------------------------------------------
It might be some obvious mistake; I'm just a beginner.
I've been looking at it for some time and can't find out what the
error is. It seems to me that are_anagrams() always returns TRUE and I
can't understand why.
I'd appreciate any help you could give me.
Thanks in advance.