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: 3547569 • 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.


USE THE FOLLOWING INPUT DATA TO TEST YOUR PROGRAM:

Note that the data is left justified, that is, if a word is not exactly 9 characters in length, blank

spaces are filled in on the right. This is done because blanks are less than any letter.

SAMPLE INPUT

SALESMAN MONTH PROGRAM WRITE

SEE THE BLUE SKY

CAT THROUGH A RAN

THE TOP TUMBLED TO

WHENEVER WATER WAS WHITE

A BLUE CLEAR SKY

SAMPLE OUTPUT

Check your answers against the following before handing in or finishing your lab. The spacing

of the output must be as shown below.

MONTH PROGRAM SALESMAN WRITE

BLUE SEE SKY THE

A CAT RAN THROUGH

THE TO TOP TUMBLED

WAS WATER WHENEVER WHITE

A BLUE CLEAR SKY

NUMBER OF CARDS = 6

NUMBER IN ORDER = 1

Explanation / Answer

PArt A



#include<stdio.h>

#include<string.h>

#include<math.h>


int main(){

int no_test_cases,len1,len2,temp,i,j;

char str1[100],str2[100];


//scanf("%d",&no_test_cases);


//while(no_test_cases--){

int arr1[26] = {0},arr2[26] = {0},flag = 0;

fflush(stdin);

//gets(str1);

//gets(str2);

scanf("%s",str1);

scanf("%s",str2);

len1 = strlen(str1);

len2 = strlen(str2);


for(i = 0;i<len1;i++){

temp = str1[i];

switch(temp){

case 65:

case 66:

case 67:

case 68:

case 69:

case 70:

case 71:

case 72:

case 73:

case 74:

case 75:

case 76:

case 77:

case 78:

case 79:

case 80:

case 81:

case 82:

case 83:

case 84:

case 85:

case 86:

case 87:

case 88:

case 89:

case 90:

arr1[temp-65] +=1;

break;


case 97:

case 98:

case 99:

case 100:

case 101:

case 102:

case 103:

case 104:

case 105:

case 106:

case 107:

case 108:

case 109:

case 110:

case 111:

case 112:

case 113:

case 114:

case 115:

case 116:

case 117:

case 118:

case 119:

case 120:

case 121:

case 122:

arr1[temp - 97]+=1;

break;

}

}


for(i = 0;i<len2;i++){

temp = str2[i];

switch(temp){

case 65:

case 66:

case 67:

case 68:

case 69:

case 70:

case 71:

case 72:

case 73:

case 74:

case 75:

case 76:

case 77:

case 78:

case 79:

case 80:

case 81:

case 82:

case 83:

case 84:

case 85:

case 86:

case 87:

case 88:

case 89:

case 90:

arr2[temp-65] +=1;

break;


case 97:

case 98:

case 99:

case 100:

case 101:

case 102:

case 103:

case 104:

case 105:

case 106:

case 107:

case 108:

case 109:

case 110:

case 111:

case 112:

case 113:

case 114:

case 115:

case 116:

case 117:

case 118:

case 119:

case 120:

case 121:

case 122:

arr2[temp - 97]+=1;

break;

}

}

for(i = 0;i<26;i++){

if(arr1[i] != arr2[i]){

flag = 1;

}

}


if(flag == 0){

printf("YES Anagram ");

}

else{

printf("NO not anagram ");

}

//}

return 0;

}