Write a C++ program where Two or more strings are anagrams if they contain exact
ID: 667284 • 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.
Note also that programs with . h at the end won't be helpful for don't have a way to use it on my laptop to look it over.
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:
csci> agm
Enter two lines that might be anagrams:
--> Eleven plus two
--> Twelve plus one
The two strings ARE anagrams.
csci> agm
Enter two lines that might be anagrams:
--> This is a string.
--> Another string
The two strings are NOT anagrams.
csci> agm
Enter two lines that might be anagrams:
--> Snooze alarms
--> Alas, no more Z's.
The two strings ARE anagrams.
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.
Explanation / Answer
/*************************************************
Student: xxxxx
ID number: xxxxx
Instructor: Dr. Julstrom
Class: CSCI 301
Project 1
*************************************************/
#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;
const int SIZE = 61;
int getinput(char []);
void compare(char [], char [], int, int);
/*************************************************
The purpose of this program is to have a user input
2 sets of different characters, either let it be just
plain characters or a sentence of a mixture of everything,
the program will collect the alphabetical character
and return a state whether the characters match or not
regardless of order.
*************************************************/
int main()
{
char sentence1[SIZE], sentence2[SIZE];
/*************************************************
a void type function compare is called passing 2
arrays and 2 integer as parameter. The parameter
getinput(sentence1) and getinput(sentence2) will
be evaluated separately.
*************************************************/
compare(sentence1, sentence2, getinput(sentence1), getinput(sentence2));
return 0;
}
int getinput(char sentence[])
{
/*************************************************
The sentence array will store any value input by
the user that is a alphabetical character and keep
track of how many time it is stored by the integer i.
the returning value after the user hits the enter key
is i.
*************************************************/
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)
{
/*************************************************
2 local character array is declared along with misc
counters: a and b will increment store1 and store2
while j will increment if a and b are incremented.
another set of counters are also declared to count
the arrays in the function parameter.
*************************************************/
char store1[SIZE];
char store2[SIZE];
int a = 0, b = 0, j = 0;
int count = 0;
int count2 = 0;
/*************************************************
The first test, if i and k is zero from the pass
function parameter, then the input from the user
is indeed an anagram.
*************************************************/
if(i == 0 && k == 0)
{
cout << "test 1" << endl; //test
cout << "The stuff you typed in is an anagram" << endl;
exit(1);
}
/*************************************************
while count(initially 0) is less than
i(integer passed by parameter);
*************************************************/
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++;
}
}
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++;
}
}
cout << "test 4" << endl; //test
cout << "The stuff you typed in is not an anagram" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.