Latest :

C Program : Find Longest Palindrome in An Array | C Programs

Find the longest palindrome element in the given array in C Programming. We can call a number a palindrome when the number is equal to the reverse of the same number i.e., when read from forward or backward it is the same.

For this, the inputs necessary are the size of the array or the number of elements in the array along with the data values or elements of the array. The expected output is a number or element from the array which is the largest palindrome among the other elements or a message stating that there are no palindromes in the given array.

The first step is always about gathering the necessary inputs. So, to read our required input at runtime, we can make use of scanf() function. This function helps us read primitive datatype inputs at runtime from the keyboard.

On the basis of the format specifier mentioned, the input datatype is decided. Since all our necessary inputs are of integer type, we will be using ‘%d’ format specifier. We first will read the size of the array or number of elements of the array and then, create an array of the mentioned size. Later, we iterate through the array from beginning to the end and read each data value or element of the array.

printf(“Enter number of elements in an array : ” );

scanf(“%d”,&n);

int a[n];

printf(“Enter %d positive elements :\n”,n);

for(i = 0; i < n; i++) {

scanf(“%d”,&a[i]);

}

Then, we make a function call to a user-defined function (longestPalindrome). To this function, we pass the input array and its size as parameters and it returns an integer denoting the index value. This user-defined function (longestPalindrome) contains the logic to find the longest palindromic element in the given array.

index=longestPalindrome(a,n);

In this function, we first store the array passed as parameter (p) into another array (a). We do so because, we need to preserve the original values to find whether they are palindrome or not and display the longest palindrome.

for(i = 0; i < n; i++) {

a[i]=p[i];

}

Later, we traverse through the array from beginning to the end and for individual element we will reverse it.

To reverse it, we will need a separate variable (rev) to store the reversed value and also store the value at that index in a temporary variable (temp) so that, the original value is preserved.

Then, we iterate until the a[i] value becomes zero and for each iteration, we first find the last digit (remainder when divided by zero) and multiply the rev with 10 before adding this last digit. Then, we divide the a[i] value by 10 to discard the last digit that is already added and to proceed with the next iteration.

By the end of this iteration the value in rev is the reversed value at that index.

rev=0;

temp=a[i];

while(a[i]!=0) {

x=a[i]%10;

rev=rev*10+x;

a[i]/=10;

}

Output:

Longest Palindrome in An Array – Type – 2

Above we have seen the logic to find the longest palindrome in a given array using functions. The main advantage of using functions is that, it makes the code reusable and also improves the readability of the code. If some part of the code is to be searched for then, we can directly go to the respective function block and search instead of going through entire main method.

Also, if the same logic is required elsewhere then, we can call the function directly and pass required parameters instead of rewriting the same set of statements.

But the time and space complexity in both the case is the same. Hence, if we are sure that, we do not require this logic anywhere else in the code then, there is no harm in writing everything within main method itself.

We can place the inputs statements, the logic as well as, the output statements all inside main method. In the below code we can see the same.

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...