Created
December 21, 2013 22:27
-
-
Save Zulqurnain/a1993db74c3072ef08d1 to your computer and use it in GitHub Desktop.
Find The First 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* substring(char* s){ | |
| char* temp; | |
| int location = 0,j=0; | |
| int size = 0; | |
| for ( int i = 1; i < strlen(s) - 1; i++ ) { | |
| int left = i; | |
| int right = i; | |
| int count = 0; | |
| while ( left >= 0 ) { // note here 1st index and last index should also be compared ! | |
| if ( s[left--] != s[right++] ) { | |
| break; | |
| } | |
| count++; | |
| } | |
| if ( count > 1 ){ | |
| size=count; | |
| location=i; | |
| break; | |
| } | |
| } | |
| int start = location - size,end = location + size; | |
| 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[] = "12454676"; | |
| cout<<substring(input); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment