Respuesta :
Answer:
/*
Problem; to finf s2 strings are anagrams.
(s2 strings are anagrams if they contain exactly the same letters, ignoring capitalization, punctuation,)
and spaces.
Solution: we must take account wich letters are present in each string and how many times. To achieve that we
may create a struct with a char and a char count, some constructors, both geters and an equal operator.
*/
#include <iostream>
#include <vector>
using namespace std;
struct charCount {
private: // Internal data structure:
char letter; // letter (should be unique)
unsigned int letterCount; // and is present at least 1 time
public:
charCount() {} // Default Constructor
charCount(char l) {letter = l; letterCount = 1;} // Constructor
charCount(const charCount & other) {letter = other.letter;
letterCount = other.letterCount;} // Copy Constructor
void increments() {letterCount++;} // Counts other occurrence
char ltr() {return(letter);} // Geter
unsigned int ltrCount() {return(letterCount);} // Geter
bool operator ==(charCount other) {return(letter == other.letter // Equal means same char and
&& letterCount == other.letterCount);}// same count.
};
/*
We have to analyze each input string returning an ordered vector of charCount elements:
*/
vector<charCount> analysis(string s) { // Raw string data
vector<charCount> result; // Result vector
for(char c:s) { // For each char of string: we take a-z
if(isalpha(c)) { // only into account and we normalize it
char x = tolower(c); // to lowercase, store as x and we are
bool found = false; // ready to search
unsigned int pos = 0; // from first position to last
for(auto & y:result) { // for each vector's element
if(y.ltr() == x) { // if we find x, we must
y.increments(); // count this occurrence,
found = true; // turn on the found flag
break; // and exit from the loop.
} else if(y.ltr() < x) { // Otherwise may be we are not going to
result.emplace(result.begin()+(pos),(charCount(x))); // find it in the ordered vector: we insert
found = true; // it at current vector's position. It is
break; // present now: we should exit from loop.
} else pos++; // Otherwise we increment pos and go on.
}
if(!found) { // x doesn't belongs to vector's set: we
auto it = result.end(); // add it at last position, keeping order
result.insert(it,(charCount(x))); // and we may go on for another cycle if any.
}
}
}
return result;
}
template <typename T> // Generic code follows to implement
bool operator ==(vector<T> v1,vector<T> v2) { // 2 vectors comparison: they are
bool result = (v1.size() == v2.size()); // equals if their sizes match,
if(result) // and each element from first vector
for(unsigned int i = 0; i < v1.size();i++) // matches with each element from the
result = result && v1[i] == v2[i]; // second vector at the same position.
return(result);
}
template bool operator ==(vector<charCount>,vector<charCount>); // Orders compiler to generate a charCount version.
bool areAnagrams(string s1,string s2) { // Anagrams implies 2 identical analysis vectors.
return(analysis(s1) == analysis(s2));}
int main(int argc, char *argv[]) { // Test code folloes:
string s1,s2; // 2 strings to store input data.
cout << "Text 1:"; getline(cin,s1); // Reads string 1 till <Enter> key.
cout << "Text 2:"; getline(cin,s2); // Reads string 2 till <Enter> key.
cout << (areAnagrams(s1,s2)?"Are":"Are not") << " anagrams..." << endl; // Are they? We use the ternary operator.
return 0;
}
Explanation: included inside the code as comments.
Answer:
#include <iostream>
using namespace std;
void areAnagrams(char str1[], char str2[])
{
int i, flag = 1, count1[26] = {0}, count2[26] = {0};
for(i = 0; str1[i] != '\0'; i++) {
if (isalpha(str1[i])) {
str1[i] = tolower(str1[i]);
count1[str1[i] - 'a']++;
}
}
for(i = 0; str2[i] != '\0'; i++) {
if (isalpha(str2[i])) {
str2[i] = tolower(str2[i]);
count2[str2[i] - 'a']++;
}
}
for(i = 0; i < 26; i++) {
if (count1[i] != count2[i])
flag = 0;
}
if (flag == 0)
cout << "The two strings are NOT anagrams";
else
cout << "The two strings are anagrams";
}
int main ()
{
char str1[100], str2[100];
cout << "Enter the first string: ";
gets(str1);
cout << "Enter the second string: ";
gets(str2);
areAnagrams(str1, str2);
return 0;
}
Explanation:
Inside the function:
- In the first loop, each character of the first string is checked if it is a letter. If it is, lower the character and increment its frequency by 1.
- In the second loop, each character of the second string is checked if it is a letter. If it is, lower the character and increment its frequency by 1.
- In the third for loop, compare the frequencies
- If they are equal, that means the strings are anagrams