Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Discrete and Combinatorial Mathematics An Applied Introduction

Authors: Ralph P. Grimaldi

5th edition

201726343, 978-0201726343

More Books

Students also viewed these Programming questions

Question

9. When should you use email to distribute your messages?

Answered: 1 week ago