Given a string, our task is to print the characters present at prime index.
Input : I love programming Output : lv gan Explanation : prime index characters in a string are : lv gan Input : Happy coding everyone Output : apycn ro
- 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 :
For large strings, we can use Sieve of Eratosthenes to efficiently find all prime numbers smaller than or equal to length of string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rearrange the given string such that all prime multiple indexes have same character
- Find the sum of the ascii values of characters which are present at prime positions
- 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
- Print characters having prime frequencies in order of occurrence
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Prime Numbers present at Kth level of a Binary Tree
- Java program to print all duplicate characters in a string
- Find the character in first string that is present at minimum index in second string
- Most frequent word in first String which is not present in second String
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Efficient program to print all prime factors of a given number
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Java program to read all Emails present in a Given file
- Check if the given string of words can be formed from words present in the dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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