please someone edit this code for me.
I do not know how to write the logic part in the function 'TestAnagram'
when i debug it, it shows the same word twice instead of showing two different words using the same letters in different order .

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int TestAnagram (char []); // function prototype
void main()
{
char StringA [256];
cout << "Enter a word " << endl;
cin >> StringA; 
cout << StringA; // echo print the word

cout << StringA << (TestAnagram(StringA) ?
". is anagram\n" :
". is NOT anagram\n");

cout << "End program ";
getch();
}

int TestAnagram(char TempString [])
{
const int False = 0,True = 1;
int i, length, CheckAnagram, leftName = 0, rightName = 0;
i = 0; 
length = strlen(TempString) - 1; 
CheckAnagram = True; 
while (leftName != rightName)
{

	if (TempString[i] == TempString[length])
		{
			
		cout << " " << CheckAnagram;
		
		}
	else
		CheckAnagram = False;
}
		// end loop
return CheckAnagram;
}

Recommended Answers

All 7 Replies

First, I assume that you mean palindrome, not anagram?
A suggestion, use std::string since you #include <string> , it will make your life easier. Especially since if you #include <algorithm> there is the option to use std::reverse .

There are other things about your code that are curious, too. Using an int to define True and False instead of using the bool type with true and false. But these are mostly nit-picks.

Along with what L7 mentioned, in your while loop leftName and rightName isn't updated and neither are your i or length variables.

Also, you'd never actually begin the loop, since you're testing 0 != 0. If you are doing an anagram, are you just trying to scramble the characters in the string or do you want it to be a meaningful word?

i edited the code.
please help me correct the TestAnagram fumction

#include <iostream>
#include <string>
#include <conio.h>
using

 namespace

 std;
int

 TestAnagram (char

 []); // function prototype


void

 main()
{
char

 StringA [256];
char

 StringB [256];





cout << "Enter 2 words "

 << endl;


cin >> StringA;


cin >> StringB; 


cout << StringA << " " << StringB; // echo print the word



cout << StringA << " " << StringB << (TestAnagram(StringA) ?
". are anagram\n"

 :


". are NOT anagram\n"

);





cout << "End program "

;


getch();


}





int

 TestAnagram(char

 TempString [])


{


const

 int

 False = 0,True = 1;


int

 i, length, CheckAnagram;


i = 0; 


length = strlen(TempString) - 1; 


CheckAnagram = True; 


while

 (StringA == StringB)


{





	if

 (TempString[i] == TempString[length])


		{


			


		cout << " "

 << CheckAnagram;


		


		}


	else


		CheckAnagram = False;
}
		// end loop


return

 CheckAnagram;


}

There's no way in hell I'm going to look at code with spacing like that. Get rid of the blank lines and add some indentation, etc.

In addition, answer the question about whether we're dealing with anagrams or palindromes, tell us what the errors are, etc.

it is for anagram
i am not getting the function TestAnagram right. it is showing the words without checking weather they are anagram or not. so the program stops there.

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int TestAnagram (char[]); // function prototype


void main()
{
char StringA [256];
char StringB [256];
cout << "Enter 2 words "<< endl;

cin >> StringA;
cin >> StringB;

cout << StringA << " " << StringB; // echo print the word

cout << StringA << " " << StringB << (TestAnagram(StringA) ?
". are anagram\n" :
". are NOT anagram\n");

cout << "End program ";

getch();
}
int TestAnagram(char TempString [])
{

const int False = 0,True = 1;
int i, length, CheckAnagram;
i = 0; length = strlen(TempString) - 1;
CheckAnagram = True;

while (StringA == StringB)
{
if (TempString[i] == TempString[length])

{

cout << " " << CheckAnagram;

}

else
CheckAnagram = False;
}
// end loop
return CheckAnagram;

}

There's no way in hell I'm going to look at code with spacing like that. Get rid of the blank lines and add some indentation, etc.

Notice indentation! See this

You keep saying anagram while your code is seemingly working toward a solution for palindromes.
An anagram is a set of letters that, when arranged another way, makes another word. A palindrome is a word that reads the same forward and back.
I've given you a hint on how to check for palindrome. For anagram, consider what is the same about versions of the letters - they are the same set, yes? How can you arrange two sets such that, if they include the same elements, they read the same? Think how a phone book is arranged...

In either case, I can tunderstand why you have not switched to using std::string

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.