1

For anyone that might be able to help me figure this out. I am creating a method that will compare two strings and detect whether they are an anagram or not. An anagram is two strings that have the same letters, though they may be in a different order. For example "listen" and "iltsen" are anagrams.

I have decided to break the strings up into char arrays. I know that is working correctly because I tested it using cout on each array element. Next is where it goes wrong. I attempt to use the ASCII value of each char and add it to a variable for each array. This would mean that if the values match then they must be an anagram.

However for whatever unknown reason it is not working correctly. I am finding that it is reading index 0 twice for one array and not for the other. I am so confused beyond reason. I have absolutely no idea what this is occurring. I have tried multiple different solutions and had no luck finding out the problem. If anyone has any idea whats going on here I would greatly appreciate the help.

-Thanks!

#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
#include <cctype>
#include <vector>
using namespace std;

bool isAnagram(string s1,string s2)
{

static char firstString[] = { 'c' };
static char secondString[] = { 'c' };
int size = s1.length();
static int count1 = 0;
static int count2 = 0; 
cout << s1 << endl;
cout << s2 << endl;
if (s1.length() == s2.length())
{
    for (int i = 0; i < size; i++)
    {
        firstString[i] = s1.at(i);
        cout << i;
    }
    for (int i = 0; i < size; i++)
    {
        secondString[i] = s2.at(i);
        cout << i;
    }
    cout << endl;

    for (int i = 0; i < size; i++)
    {
        count1 = count1 + (int)firstString[i];
        cout << "first" << i << ": " << firstString[i] << " = " <<         (int)firstString[i] << endl;
        count2 = count2 + (int)secondString[i];
        cout << "second" << i << ": " << secondString[i] << " = " << (int)secondString[i] << endl;
    }
    cout << count1 << " and " << count2 << endl;
    if (count1 == count2)
        return true;
}
else
    return false;

count1 = 0;
count2 = 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
static char end;
do
{
    string s1;
    string s2;

    cout << "Please enter the first string: ";
    cin >> s1;
    cout << endl << "Please enter the second string: ";
    cin >> s2;

    bool result = isAnagram(s1, s2);
    static string resultString;

    if (result == true)
        resultString = "True";
    else
        resultString = "False";

    cout << endl << "Anagram test result: " << resultString << endl;
    cout << endl << "enter E for end or any other key to run again: ";
    cin >> end;
    cout << "----------------------------------------" << endl;
} while (end != 'e' && end != 'E');

return 0;
} 
8
  • Why are the arrays and counts static? And you should be using std::vectors and not arrays Commented Dec 12, 2016 at 16:31
  • firstString[i] = s1.at(i); you can't do that: buffer is too small. Commented Dec 12, 2016 at 16:31
  • 2
    Already exists in the standard library :std::is_permutation Commented Dec 12, 2016 at 16:36
  • I made them static so that their values can be modified within the loops considering they would be out of scope. Is this a correct assumption? Also Jean, thank you for your reply. Is there a way to set the buffer to a larger size? And Paul, thank you as well for your response. The link you provided is very helpful.
    – Wozzey
    Commented Dec 12, 2016 at 16:46
  • You need to check that the lengths of s1 and s2 are the same. After that allocate your two char arrays to that size.
    – stark
    Commented Dec 12, 2016 at 16:53

1 Answer 1

1

It's not useful to use static variables in your case, without them you wouldn't need the last 2 lines of isAnagram. It's also useless to store both strings in char arrays because you can use them directly in your 3rd loop(also you are overflowing your buffer which has a size of 1)

for (int i = 0; i < size; i++)
{
    std::cout << count1 << " ";
    count1 = count1 + (int)s1.at(i);
    cout << "first" << i << ": " << s1.at(i) << " = " << (int)s1.at(i) << endl;
    count2 = count2 + (int)s2.at(i);
    cout << "second" << i << ": " << s2.at(i) << " = " << (int)s2.at(i) << endl;
}

Also you can't say that 2 strings do contain the same letters by comparing the sum of their ASCII values, thats like saying 3 + 4 is the same as 2 + 5 because both give 7. You could create an array of 52 ints, each element is a counter for its own letter, then you could loop over both strings with one loop where each letter of the first string is incrementing its element in the array and the second strings letters are decrementing elements.

if (s1.length() != s2.length())
    return false;

std::vector<int> counts(52);

for (unsigned int i = 0; i < s1.length(); ++i)
{
    ++counts[s1[i] - (s1[i] < 91 ? 65 : 71)];
    --counts[s2[i] - (s2[i] < 91 ? 65 : 71)];
}

be sure that the array is initialized to 0. At the end you need to loop over the array, if one of the elements is not 0 it's not an anagram, else return true.

for (unsigned int i = 0; i < 52; ++i)
    if (counts[i] != 0)
        return false;

return true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.