Advertisement
BORUTO-121

longestPalindrom

Sep 27th, 2022 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. char toLower(char letter){
  5.     return (letter>='a' && letter<='z') ? letter : letter+'a'-'A';
  6. }
  7.  
  8. int isPalindrom(char *start, char *end){
  9.     if(start>end || (start==end && toLower(*start)==toLower(*end)))return 1;
  10.     else if(toLower(*start)!=toLower(*end))return 0;
  11.     return isPalindrom(start+1,end-1);
  12. }
  13.  
  14. void longestPalindrom(char *sen){
  15.     char *end=sen+strlen(sen)-1;
  16.     //Helping pointers
  17.     char *start,*helpEnd,*finalStart=sen+1,*finalEnd=sen;
  18.  
  19.     for(start=sen;start<=end;start++){
  20.         for(helpEnd=end;helpEnd>=start && !isPalindrom(start,helpEnd);helpEnd--);
  21.         if(helpEnd>start && (helpEnd-start)>(finalEnd-finalStart))
  22.             finalEnd=helpEnd,finalStart=start;
  23.     }
  24.  
  25.     for(;finalStart<=finalEnd;finalStart++)
  26.         printf("%c",*finalStart);
  27. }
  28.  
  29. int main(){
  30.     //Test
  31.     longestPalindrom("");
  32.     longestPalindrom("yxpxYstsyxpx");
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement