Program to print characters present at prime indexes in a given string
Given a string, our task is to print the characters present at prime index.
Examples :
Input : I love programming Output : lv gan Explanation : prime index characters in a string are : lv gan Input : Happy coding everyone Output : apycn ro
Approach :
- Use two loops to divide the numbers upto the value of length of string.
- Increment variable result when the remainder is 0.
- If the variable result = 1, then print the corresponding character.
Below is the implementation of above approach :
C++
// C++ Program to print Characters at // Prime index in a given String #include <bits/stdc++.h> using namespace std; bool isPrime( int n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for ( int i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function to print // character at prime index void prime_index(string input) { int n = input.length(); // Loop to check if // index prime or not for ( int i = 2; i <= n; i++) if (isPrime(i)) cout << input[i - 1]; } // Driver Code int main() { string input = "w3wiki" ; prime_index(input); return 0; } |
Java
// Java Program to print // Characters at Prime index // in a given String class GFG { static boolean isPrime( int n) { // Corner case if (n <= 1 ) return false ; // Check from 2 to n-1 for ( int i = 2 ; i < n; i++) if (n % i == 0 ) return false ; return true ; } // Function to print // character at prime index static void prime_index(String input) { int n = input.length(); // Loop to check if // index prime or not for ( int i = 2 ; i <= n; i++) if (isPrime(i)) System.out.print (input.charAt(i - 1 )); } // Driver code public static void main (String[] args) { String input = "w3wiki" ; prime_index(input); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to print # Characters at Prime index # in a given String def isPrime(n): # Corner case if n < = 1 : return False # Check from 2 to n-1 for i in range ( 2 , n): if n % i = = 0 : return False ; return True # Function to print # character at prime index def prime_index ( input ): p = list ( input ) s = "" # Loop to check if # index prime or not for i in range ( 2 , len (p) + 1 ): if isPrime(i): s = s + input [i - 1 ] print (s) # Driver Code input = "w3wiki" prime_index( input ) |
C#
// C# Program to print Characters // at Prime index in a given String using System; class GFG { static bool isPrime( int n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for ( int i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function to print character // at prime index static void prime_index( string input) { int n = input.Length; // Loop to check if // index prime or not for ( int i = 2; i <= n; i++) if (isPrime(i)) Console.Write(input[i - 1]); } // Driver code public static void Main () { string input = "w3wiki" ; prime_index(input); } } // This code is contributed by Vt_m. |
PHP
<?php // PHP Program to print // Characters at Prime // index in a given String function isPrime( $n ) { // Corner case if ( $n <= 1) return false; // Check from 2 to n-1 for ( $i = 2; $i < $n ; $i ++) if ( $n % $i == 0) return false; return true; } // Function to print // character at prime index function prime_index( $input ) { $n = strlen ( $input ); // Loop to check if // index prime or not for ( $i = 2; $i <= $n ; $i ++) if (isPrime( $i )) echo $input [ $i - 1]; } // Driver Code $input = "w3wiki" ; prime_index( $input ); // This code is contributed by ajit. ?> |
Javascript
<script> // Javascript program to print characters // at Prime index in a given String function isPrime(n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for (let i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function to print character // at prime index function prime_index(input) { let n = input.length; // Loop to check if // index prime or not for (let i = 2; i <= n; i++) if (isPrime(i)) document.write(input[i - 1]); } // Driver code let input = "w3wiki" ; prime_index(input); // This code is contributed by suresh07 </script> |
Output
eesoes
Time Complexity: O(n2), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Optimizations :
For large strings, we can use Sieve of Eratosthenes to efficiently find all prime numbers smaller than or equal to length of string.