Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function called isAnagram that takes two strings as arguments and return

ID: 3730888 • Letter: W

Question

Write a function called isAnagram that takes two strings as arguments and returns a boolean true if the two strings are anagrams, otherwise it returns false. Two strings are anagrams if the letters can be rearranged to form each other. You MUST use the C string library or string class functions to complete this code. (no sort functions)

MAKE SURE THE FUNCTION IS NOT CASE SENSITIVE AND SHOULD DISREGARD ANY PUNCTUATION OR SPACES.

#include <iostream>

#include <string>

#include <cstring>

#include <algorithm>

using namespace std;

bool isAnagarm (string s1, string s2){

“Rats and Mice” as string s1 and “in cat’s dream” as string s2 should also return true.

To test the code enter "Eleven plus two” as string s1 and “Twelve plus one” as string s2. The function should return true.

Explanation / Answer

#include <stdio.h>

bool check_anagram(char [], char []);

int main()
{
char a[100], b[100];
int flag;

printf("Enter first string ");
gets(a);

printf("Enter second string ");
gets(b);

flag = check_anagram(a, b);

if (flag == 1)
printf("%s and %s are anagrams. ", a, b);
else
printf("%s and %s are not anagrams. ", a, b);

return 0;
}
void remove_punct_and_make_lower_case(char *p)
{
char *src = p, *dst = p;

while (*src)
{
if (ispunct((unsigned char)*src) || isspace((unsigned char)*src))
{
/* Skip this character */
src++;
}
else if (isupper((unsigned char)*src))
{
/* Make it lowercase */
*dst++ = tolower((unsigned char)*src);
src++;
}
else if (src == dst)
{
/* Increment both pointers without copying */
src++;
dst++;
}
else
{
/* Copy character */
*dst++ = *src++;
}
}

*dst = 0;
}

bool check_anagram(char a[], char b[])
{
remove_punct_and_make_lower_case(a);
remove_punct_and_make_lower_case(b);
  
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '')
{
first[a[c]-'a']++;
c++;
}

c = 0;

while (b[c] != '')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return false;
}

return true;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote