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

ANAGRAM.CPP Write a function in c++ that determines if two strings are anagrams.

ID: 3845118 • Letter: A

Question

ANAGRAM.CPP

Write a function in c++ that determines if two strings are anagrams. The function should not be case sensitive and should disregard any punctuation or spaces. Two strings are anagrams if the letters can be rearranged to form each other. For example, “Eleven plus two” is an anagram of “Twelve plus one”. Each string contains one “v”, three “e’s”, two “l’s”, etc. You may use either the string class or a C-style string. Either way, you may not use built-in C++ functions that we have NOT discussed in lecture.

Write a program in C++ that inputs two strings and calls your function to determine whether or not the strings are anagrams and prints the result.

REQUIRED: You MUST use strings to write this program. You may not use any C++ functions that we have NOT discussed in lecture. The program should print a string of text to the terminal before getting each line of input from the user. Try to use more simple functions.

A session should look EXACTLY like one of the following examples (including whitespace and formatting), with possibly different numbers and numbers of asterisks in the output:

OR

The strings printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

Explanation / Answer

#include <iostream>
#include <cstring>
using namespace std;

int checkAnagram(char *f, char *s);

int main(){
char f1[100], s1[100];
cout << "Enter first String: "; //aceepting the first string from user
cin.getline(f1, 100);

cout << "Enter second String: "; //accepting the second true from user
cin.getline(s1, 100);
  
if(checkAnagram(f1, s1)){ //calling the function
cout << "The strings are Anagram";
} else {
cout << "Thestrings are not Anagram";
}

return 0;
}
int checkAnagram(char *f, char *s){
int fc[256] = {0}, sc[256] = {0};
int i;
  
if(strlen(f) != strlen(s)){ //checking if two strings are equal
return 0;
}

  
for(i = 0; f[i] != ''; i++){
fc[f[i]]++;
}

for(i = 0; s[i] != ''; i++){
sc[s[i]]++;
}
  
for(i = 0; i < 256; i++){
if(fc[i] != sc[i])
return 0; //return the value on condition check
}

return 1; //return the value on condition check
}

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