Skip to content

Instantly share code, notes, and snippets.

@Zulqurnain
Created December 21, 2013 22:12
Show Gist options
  • Save Zulqurnain/974f606bf3033897058c to your computer and use it in GitHub Desktop.
Save Zulqurnain/974f606bf3033897058c to your computer and use it in GitHub Desktop.
Find The Longest Palindrome sub string in a Given string.
#include <iostream>
#include <string.h>
using namespace std;
char* Longpali(char* s){
char* temp;
int location = 0,j=0;
int maxsize = 0;
for ( int i = 0; i < strlen(s) - 1; i++ ) {
int left = i;
int right = i;
int count = 0;
while ( left > 0 ) {
if ( s[left--] != s[right++] ) {
break;
}
count++;
}
if ( count > maxsize ) { // FINDING MAX counter
maxsize = count;
location = i;
}
}
int start = location - maxsize,end = location + maxsize;
temp=new char [strlen(s)+1];
for (i = start + 1; i < end; i++ ) {
temp[j++]=s[i];
}
temp[j]=0;
return temp;
}
int main() {
char input[] = "A5AAABAAA5A";
cout<<Longpali(input);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment