Created
December 21, 2013 22:12
-
-
Save Zulqurnain/974f606bf3033897058c to your computer and use it in GitHub Desktop.
Find The Longest Palindrome sub string in a Given string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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