Question: Rewrite the following Python anagram function to C++: def anagram(s1, s2) : c1 = [0] * 26 c2 = [0] * 26 for i in

Rewrite the following Python anagram function to C++:

def anagram(s1, s2) : c1 = [0] * 26 c2 = [0] * 26

for i in range(len(s1)) : pos = ord(s1[i]) - ord('a') c1[pos] = c1[pos] + 1

for i in range(len(s2)) : pos = ord(s2[i]) - ord('a') c2[pos] = c2[pos] + 1

j = 0 stillOK = True while j if c1[j] == c2[j] : j = j + 1 else : stillOK = False

return stillOK

The following C++ code is my attempt at rewriting it. I feel like I'm really close, but I'm getting an error on the expression \"c1 = s1[0] * 26;\". I'm sure there's more errors beyond it, but they might be similar in nature. My code:

#include #include #include using namespace std;

bool anagram(const string s1, const string s2) { vectorc1; vectorc2; unsigned int pos = 0; bool stillOK = true; int j = 0;

c1 = s1[0] * 26; // error on this line c2 = s2[0] * 26; // error on this line

for (int i = 0; i int pos = 0; pos = (int)(s1[i]) - (int)'a'; c1[pos] = c1[pos] + 1; }

for (int i = 0; i pos = (int)(s2[i]) - (int)'a'; c2[pos] = c2[pos] + 1; }

j = 0; stillOK = true; while (j if (c1[j] == c2[j]) j = j + 1; else stillOK = false; return stillOK; } } int main() { anagram(\"foo\", \"oof\"); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!