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

I am trying to write a C++ code to determine if any permutation of a phrase (ign

ID: 3737869 • Letter: I

Question

I am trying to write a C++ code to determine if any permutation of a phrase (ignoring whitespace and punctuation). I am required to use std::unordered_set or std::unordered_map. The input contains separate phrases on each line. An example of a run of this program should look like

Given the following input:

civic
ivicc
civil
livci

The program should output the following:

"civic" is a palindrome permutation
"ivicc" is a palindrome permutation
"civil" is not a palindrome permutation
"livci" is not a palindrome permutation

Explanation / Answer

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
bool is_palindrome_permutation(char* p)
{
unordered_map<char, size_t> m;
while (*p)
{
if (isalpha(*p))
{
m[*p]++;
}
p++;
}
size_t count = 0;
for (auto& p : m)
{
if (p.second % 2 == 1)
{
if (++count == 2)
{
return false;
}
}
}
return true;
}
int main(){
int n,i;
cout<<"Enter no. of strings : ";
cin>>n;
for(i=0;i<n;i++){
char a[50];
cout<<"Enter string : ";
cin>>a;
if(is_palindrome_permutation(a)){
cout<<a<<" is a palindrome permutation. "<<endl<<endl;
}
else{
cout<<a<<" is not a palindrome permutation. "<<endl<<endl;
}
}
  
return 0;
}

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