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

Write a C++ program where Two or more strings are anagrams if they contain exact

ID: 667247 • Letter: W

Question

Write a C++ program where Two or more strings are anagrams if they contain exactly the same letters, ignoring capitalization, punctuation, and spaces. For example, "Information superhighway" and "New utopia? Horrifying sham" are anagrams, as are "ab123ab" and "%%b b*aa". Note that two lines that contain no letters are anagrams.

DESCRIPTION

Write a program that reads two lines of input from the terminal and determines and reports if the lines are anagrams.

INPUT

Input lines will consist of letters, digits, blanks, and punctuation, like this: "New utopia? Horrifying sham". The maximum length of an input line is 60 characters.

OUTPUT

The program will prompt for two input lines and will report whether or not two lines are anagrams.

ERRORS

Your program may assume that the input is as described; it need not detect any errors.

EXAMPLE

Several runs of the program might look like this:

OTHER REQUIREMENTS

Apply top-down design techniques to design an effective algorithm and to build an intelligible program with good structure. Use functions as appropriate; your program must include at least two functions.

HINTS

Read each input line one character at a time, using cin.get(). Process only the letters. Write a function that deals with one line and call it twice.

Note that the end-of-line character has an ASCII value less than that of any printable character; it signals the end of an input line.

HAND IN

Hand in a design document, user document, annotated tests that demonstrate that the program works, and a summary of your reactions to and experience with this project, as described in About Programming Projects. Remember that the design document must begin with a description of the problem the program solves and must provide enough detail that a naive reader could construct from it a program substantially like yours.

Question: If n is the total number of characters in the two input lines, what can you say about the big-O behavior of your program's time? Justify your observations.

Question: Describe a strategy entirely different from the one you implemented that would also determine if two input lines are anagrams.

Explanation / Answer

#include <iostream>
#include <cmath>
#include <cctype>
#include<cstdlib>
using namespace std;
const int SIZE = 61;
int getinput(char []);
void compare(char [], char [], int, int);
int main()
{
char sentence1[SIZE], sentence2[SIZE];
compare(sentence1, sentence2, getinput(sentence1), getinput(sentence2));
return 0;
}
int getinput(char sentence[])
{
char ch;
int i = 0;
cout << "enter something => ";
cin.get(ch);
int count = 0;
while(ch >= ' ' && count < SIZE)
{
if (isalpha(ch))
{
sentence[i++] = ch;
}
cin.get(ch);
count++;
}
return i;
}
void compare(char sentence1[], char sentence2[], int i, int k)
{
char store1[SIZE];
char store2[SIZE];
int a = 0, b = 0, j = 0;   
int count = 0;
int count2 = 0;



int p = 0;
if(j == (i) && j == (k))
{
while(store1[p] == store2[p])
{
if(p == i && (store1[p] == store2[p]))
{
cout << "test 3" << endl; //test
cout << "The stuff you typed in is an anagram" << endl;
exit(1);
}
p++;
}
}

if(i == 0 && k == 0)
{
cout << "test 1" << endl; //test
cout << "The stuff you typed in is an anagram" << endl;
exit(1);
}
while( count < i)
{
if((count2 >= i && count == 0) && j == 0)
{
cout << "test 2" << endl; //test
cout << "The stuff you typed in is not an anagram" << endl;
exit(1);
}
if(sentence1[count] != sentence2[count2])
{
count2++;
}
else if(sentence1[count] == sentence2[count2])
{
store1[a] = sentence1[count];
store2[b] = sentence2[count2];
a++; b++; j++;
count++;
count2++;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote