Program to print characters present at prime indexes in a given string

01 May, 2021
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
 
 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
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.
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++
Java
Python3
C#
PHP
Javascript
// 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 indexvoid 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 Codeint main(){    string input = "GeeksforGeeks";    prime_index(input);    return 0;}
// Java Program to print// Characters at Prime index// in a given Stringclass 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.
# 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 indexdef 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 Codeinput = "GeeksforGeeks"prime_index(input)
// C# Program to print Characters// at Prime index in a given Stringusing 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.
<?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 indexfunction 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.?>
<script> // Javascript program to print characters// at Prime index in a given Stringfunction 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 indexfunction 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 codelet input = "GeeksforGeeks";       prime_index(input); // This code is contributed by suresh07  </script>
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.  
Program to print characters present at prime indexes in a given string | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersProgram to print characters present at prime indexes in a given string | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:18•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=luBTP8zRqMo" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
jit_t
suresh07
Prime Number
Strings
Strings
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Top 50 String Coding Problems for Interviews
What is Data Structure: Types, Classifications and Applications
Convert string to char array in C++
Check whether two strings are anagram of each other
Reverse words in a given string
Print all the duplicates in the input string
Reverse string in Python (6 different ways)
Remove duplicates from a given string