Jun 25, 2017

palindrome substring of a given string


#include <iostream>
#include <string.h>
using namespace std;

bool ispalindrome(char* s, int l, int r)
{
 int n = r-l+1;
 for(int i=0; i<n/2; i++)
 {
  if(s[l+i] != s[r-i])
   return false;
 }
 
 for(int i=l; i<=r; i++)
  cout << s[i];
 cout << endl;
 return true;
}
void palindromesubstrings(char* s)
{
 int n = strlen(s);
 for(int i=0; i<n; i++)
 {
  for(int j=i+1; j<n; j++)
  {
   if(s[i] == s[j])
   {
    //cout << "[" << i << "," << j << "]=" << s[i] << endl;
    ispalindrome(s,i,j);
   }
  }
 }
}

int main() {
 // your code goes here
 char s1[100], s2[100];
 cin >> s1;
 cin >> s2;
 //cout << "palindrome " << s1 << " " << (ispalindrome(s1,0,strlen(s1)-1)? "true" : "false");
 cout << "palindrome substrings of " << s2 << ":\n";
 palindromesubstrings(s2);
 return 0;
}

palindrome substrings of ABCBAHELLOHOWRACECARAREYOUIAMAIDOINGGOOD:
ABCBA
BCB
LL
OHO
RACECAR
ACECA
CEC
ARA
RAR
IAMAI
AMA
GG
OO

No comments: