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.
- 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
- Java program to print all duplicate characters in a string
- 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
- 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
- Program to print first N Prime numbers
- Print all distinct characters of a string in order (3 Methods)
- Python program to print all Prime numbers in an Interval
- C Program for efficiently print all prime factors of a given number
- Efficient program to 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
- Program to check if first and the last characters of string are equal
- C program to swap adjacent characters of 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 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