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 = "GeeksforGeeks"; prime_index(input); return 0; } |
chevron_right
filter_none
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 = "GeeksforGeeks"; prime_index(input); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
Python
# 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 = "GeeksforGeeks"prime_index(input) |
chevron_right
filter_none
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 = "GeeksforGeeks"; prime_index(input); } } // This code is contributed by Vt_m. |
chevron_right
filter_none
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 = "GeeksforGeeks"; prime_index($input); // This code is contributed by ajit. ?> |
chevron_right
filter_none
Output :
eesoes
Optimizations :
For large strings, we can use Sieve of Eratosthenes to efficiently find all prime numbers smaller than or equal to length of string.
Recommended Posts:
- Find the sum of the ascii values of characters which are present at prime positions
- Rearrange the given string such that all prime multiple indexes have same character
- Remove characters from the first string which are present in the second string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- XOR of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Sum and Product of Prime Frequencies of Characters in a String
- Check whether count of distinct characters in a string is Prime or not
- Count characters in a string whose ASCII values are prime
- Print all distinct characters of a string in order (3 Methods)
- Efficient program to print all prime factors of a given number
- Python program to print all Prime numbers in an Interval
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Program to toggle all characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t



