
Tausif K.
asked 12/03/20write a C program to
Design anagram game using array. Allow a user to enter N primary words and store it in an array. Generate a random number from the range 1 to N. Based on the random number generated display the string stored at that index of array and allow user to enter its anagram. Compare whether an anagram entered is correct or not and display appropriate message. [Given a word A and word B. B is said to be an anagram of A if and only If the characters present is B is same as characters present in A, irrespective of sequence. For ex: “EARTH” == “HEART”]
1 Expert Answer

Patrick B. answered 12/03/20
Math and computer tutor/teacher
I will work on this tonight.
But here is my suggestion for determining if two words are anagrams.
First of all,the string length must be the same.
Otherwise they cannot be anagrams.
If their string lengths are the same
Copies them into separate buffers
Sorts the letters in each one
Compares them side by side for match
That is a straight- forward design of the basic logic.
/*************************************************************************************************/
using namespace std;
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define MAX_WORD_LEN (25)
void Capitalize( char * str)
{
int N = strlen(str);
for (int iLoop=0; iLoop<N; iLoop++)
{
char chChar = str[iLoop];
str[iLoop]=toupper(chChar);
}
}
int CompareChar( const void * rec1,const void * rec2)
{
char * chPtr1 = (char*)rec1;
char * chPtr2 = (char*)rec2;
int iReturn =(int) ( (*chPtr1) - (*chPtr2) );
}
int AreAnagrams( char * str1, char * str2)
{
Capitalize(str1);
Capitalize(str2);
int N1 = strlen(str1);
int N2 = strlen(str2);
//cout << str1 << " has length " << N1 << endl;
//cout << str2 << " has length " << N2 << endl;
qsort(str1,N1,sizeof(char),CompareChar);
qsort(str2,N2,sizeof(char),CompareChar);
//cout << str1 << endl;
//cout << str2 << endl;
return(strcmp(str1,str2));
}
Go()
{
char * words[] = { (char*) "ANGEL",
(char*) "BELOW",
(char*) "CIDER",
(char*) "DUSTY",
(char*) "ENTER",
(char*) "FIRED",
(char*) "GRAB",
(char*) "HOW",
(char*) "INS",
(char*) "JANIN",
(char*) "KIN",
(char*) "LEG",
(char*) "MUG",
(char*) "NAP",
(char*) "OPT",
(char*) "PEA",
(char*) "QUITANE",
(char*) "RAP",
(char*) "SIT",
(char*) "TEA",
(char*) "UGH",
(char*) "VERNE",
(char*) "WAR",
(char*) "X",
(char*) "YAP",
(char*) "ZERO"
};
int iIndexPos=-1;
srand (time(NULL));
while ((iIndexPos<0) || (iIndexPos>25))
{
iIndexPos = rand()%26;
}
char inbuff[MAX_WORD_LEN];
cout << "Anagram for " << words[iIndexPos] << " ---:> ";
cin >> inbuff;
char str1[MAX_WORD_LEN];
char str2[MAX_WORD_LEN];
memset(str1,0,MAX_WORD_LEN);
memset(str2,0,MAX_WORD_LEN);
strcpy(str1,words[iIndexPos]);
strcpy(str2,inbuff);
int iReturn = AreAnagrams(str1,str2);
if (iReturn==0)
{
cout << words[iIndexPos] << " and " << inbuff << " are anagrams " << endl;
}
else
{
cout << words[iIndexPos] << " and " << inbuff << " are NOT anagrams " << endl;
}
}
int main()
{
Go();
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES section12/04/20