(100 pts) Write a program to find the largest palindrome in a string. A palindro
ID: 648876 • Letter: #
Question
(100 pts) Write a program to find the largest palindrome in a string. A palindrome is a sequence that reads the same backward as forward. racecar, eve, kayak are some examples. For this assignment implement the following function int palindromelength(char *str, int i, int j) This function takes a string str and two points i and j and determines whether the string from i to j is a palindrome. If it is a palindrome, it returns the length of the palindrome and if it is not a palindrome, it returns -1. Consider the following string as an example ABCBAHELLOHOWRACECARAREYOUIAMAIDOINEVERODDOREVENNGGOOD It has many palindromes in it including ABCBA, RACECAR, ABA, IAMAL. Largest palindrome is NEVERODDOREVEN since it has the most characters in it. Sample execution is given below Enter a string ABCBACDCBAAB Largest palindrome is ABCBAExplanation / Answer
#include<stdio.h>
#include<string.h>
int palindromeLength(char *str, int i, int j){
int flag = 0,len = 0;
while(i < j){
if(str[i] == str[j]){
i++;
j--;
len += 2;;
}else{
flag = 1;
break;
}
}
if(flag == 1){
return -1;
}
if(i == j){
len++;
}
return len;
}
int main(){
char str[500];
int maxLength = 0,maxStart,maxEnd;
int i,j,length;
printf("Enter a string : ");
scanf("%s",str);
int len = strlen(str);
for(i=0; i<len; i++){
for(j = len-1; j>i; j--){
length = palindromeLength(str, i, j);
if(length > maxLength){
maxLength = length;
maxStart = i;
maxEnd = j;
}
}
}
printf("Largest palindrome is ");
for(i = maxStart; i<= maxEnd; i++){
printf("%c", str[i]);
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.