Program for unique words pair

//the task is to find the pairs ina string array of unique words such that if we combine them they form aplalindrome
#include
#include<unordered_map>
using namespace std;
bool palindrome(string arr[],int i,int j){
while(i<j){
if(arr[i]!=arr[j])
return false;
i++;
j–;
}
return true;
}

string find_reverse(string str){
int len=str.length();
string rev="";
for(int i=len-1;i>=0;i–)
rev[len-1-i]=str[i];
cout<<rev<<endl;
return rev;
}

void palindromepairs(string arr[],int n){
//storing the reverse of the strings in the hash map
unordered_map<string,int> rev;
for(int i=0;i<n;i++){
cout<<arr[i]<<endl;
string str=find_reverse(arr[i]);
cout<<str<<endl;
rev[str]=i;
}

//iterating over each string and finding whetger its reverse exists
for(int i=0;i<n;i++){
int len=arr[i].size();
cout<<len<<endl<<endl;
string sub;
//iterating over esch substring of arr[i] strating from 0 and checking whether its reverse is present or not
for(int j=0;j<len;j++){
sub[j]=arr[i][j];
cout<<sub<<endl;
if(rev.count(sub)){
if(palindrome(arr,j+1,n-1))
cout<<"[ “<<i<<” “<< rev[sub]<<” ]"<<endl;
}

}
//in the above case the arr[i] is always a prefix it does not consider the acses where arr[i] is a suffix
for(int j=len-1;j>=0;j--){
        sub[len-1-j]=arr[i][j];
        if(rev.count(sub)){
            if(palindrome(arr,0,j-1))
                cout<<"[ "<<rev[sub]<<" "<< i<<" ]"<<",";
        }


}

}
}

int main(){
int n;
cin>>n;
string * str=new string[n];
for(int i=0;i<n;i++)
cin>>str[i];
palindromepairs(str,n);
return 0;
}

what are the issues in this code?

hey @Rj.25 please share the code throught ide.codingblocks.com, it is easier to work with code their rather than here.

@Rj.25 I dont think your logic is correct. please try the following approach
1- Consider each pair one by one.
2- Check if any of the pairs forms a palindrome
after concatenating them.
3- Return true, if any such pair exists.
4- Else, return false.

please specify the mistake in the code

It doesn’t make sense for “iterating over each substring of arr[i] starting from 0 and checking whether its reverse is present or not”.

for example if you have substring abc and reverse of substring ab is present and if “c” is a palindrome if reverse of ab is present your code will accept baabc as a palindrom which it clearly is not.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.