Check if String formed by first and last X characters of a String is a Palindrome | Java program for the above approach ; Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { public static void isEqualSubstring ( String str , int x ) { int n = str . length ( ) ; int i = 0 ; while ( i < n && i < x ) { if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) { System . out . println ( " false " ) ; return ; } i ++ ; } System . out . println ( " true " ) ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; int x = 3 ; isEqualSubstring ( str , x ) ; } }