I am trying to write a code that checks if a string is an anagram or not. However I keep getting error's That "you cannot assign to a variable that is constant". I understand what it means, but what is the walkaround/solution for this?
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool check_str(const string& a, const string& b)
{
// cant be the same if the lenghts are not the same
if (a.length() != b.length())
return false;
//both the strings are sorted and then char by char compared
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (int i = 0; i < a.length(); i++)
{
if (a[i] != b[i]) //char by char comparison
return false;
}
return true;
}
int main()
{
string a = "apple";
string b = "ppple";
if (check_str(a, b))
{
cout << "Yes same stuff" << endl;
}
else
{
cout << "Not the same stuff" << endl;
}
system("pause");
}