Program to print characters present at prime indexes in a given string | Java Program to print Characters at Prime index in a given String ; Corner case ; Check from 2 to n - 1 ; Function to print character at prime index ; Loop to check if index prime or not ; Driver code
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void prime_index ( String input ) { int n = input . length ( ) ; for ( int i = 2 ; i <= n ; i ++ ) if ( isPrime ( i ) ) System . out . print ( input . charAt ( i - 1 ) ) ; } public static void main ( String [ ] args ) { String input = " GeeksforGeeks " ; prime_index ( input ) ; } }