Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A Enter two strings from keyboard and check whether the strings are anagram

ID: 3553700 • Letter: P

Question

Part A

Enter two strings from keyboard and check whether the strings are anagrams.

Part B

Character data play an important part in many computer problems. It is important to get

used to working with character data even though you will find that character data is slightly

more difficult to work with in C++ than other high level programming languages.

You are to write a program that should read in four words at a time, each word being no

longer than 9 characters. The program is to print out each set of words on a separate line, in

alphabetical order. After the last set of words has been processed the program is to print out:

a) the number of sets processed

b) the number of sets that were in alphabetical order to start with,

and needed no rearranging.

In order to simplify the reading in of the input data, a "procedure" (or subprogram) should be

prepared to do most of the work for you.

NOTES ON LOGIC

The basic algorithm for this problem is a sort routine.

1) You simply get the "largest" word into WORD4 position first.

(The first three decision steps do this.)

2) You then get the "second largest" word into WORD3 position.

(The fourth and fifth decision steps do this.)

3) And then you will get the first and second words into their proper positions.

(The sixth decision step does this.)

4) Swapping is accomplished by three steps:

a) move "high value word" to TEMP

b) move "low value word" to "low value box"

c) move "TEMP" word to "high value box"

5) The SWITCH variable is used to record the occurrence of a "swap". Since

SWITCH and "swap" means rearranging was needed, the INORDER counter

is not increased by 1 when SWITCH is still = `YES`.

If you understand the improved "bubble sort" method you can go ahead and use it.

You must enter the strings one character at a time and insert a null character at the end of

each word so that you can use the string library functions.

Explanation / Answer

#include<iostream>

#include<string>

using namespace std;


int anagram(string a, string b)

{

if(a.size() != b.size())

{

return 0;

}

int count[27],count1[27];

for(int i=0;i<27;i++)

{

count[i]=0;

count1[i]=0;

}

for(int i=0;i<a.size();i++)

{

if(a[i]==' ')

{

count[26]++;

}

else

{

count[a[i]-'a']++;

}

}

for(int i=0;i<b.size();i++)

{

if(b[i]==' ')

{

count[26]++;

}

else

{

count1[b[i]-'a']++;

}

}

for(int i=0;i<27;i++)

{

  

if(count[i]!=count1[i])

{

return 0;

}

}

return 1;

}

  

int main()

{

string a,b;

cout<<"enter string 1 : ";

getline(cin,a);

cout<<"enter string 2 : ";

getline(cin,b);

if(anagram(a,b))

{

cout<<"String A and String B are Anagram "<<endl;

}

else

{

cout<<"String A and String B are not Anagram "<<endl;

}

system("pause");

}