Given a string S. The task is to count and print the number of characters in the string whose ASCII values are prime.
Input: S = “geeksforgeeks”
Output : 3
‘g’, ‘e’ and ‘k’ are the only characters whose ASCII values are prime i.e. 103, 101 and 107 respectively.
Input: S = “abcdefghijklmnopqrstuvwxyz”
Approach: The idea is to generate all primes upto max ASCII value of character of string S using Sieve of Eratosthenes. Now, Iterate the string and get the ASCII value of each character. If the ASCII value is prime then increment the count. Finally, print the count.
Below is the implementation of the above approach:
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
- Average of ASCII values of characters of a given string
- Program to find the XOR of ASCII values of characters in a string
- Program to find the product of ASCII values of characters in a string
- Count pairs of characters in a string whose ASCII value difference is K
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Check whether count of distinct characters in a string is Prime or not
- Count the number of words having sum of ASCII values less than and greater than k
- Convert a string to hexadecimal ASCII values
- Check if a string contains only alphabets in Java using ASCII values
- Split a given string into substrings of length K with equal sum of ASCII values
- Program to find the largest and smallest ASCII valued characters in a string
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Sum of the alphabetical values of the characters of a string
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- XOR of Prime Frequencies of Characters in a String
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- 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
- Program to print characters present at prime indexes in a given 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.