Count characters in a string whose ASCII values are prime
Given a string S. The task is to count and print the number of characters in the string whose ASCII values are prime.
Examples:
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”
Output: 6
Approach: The idea is to generate all primes up to the max ASCII value of the character of string S using the 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:
C++
#include <bits/stdc++.h>
using namespace std;
#define max_val 257
int PrimeCharacters(string s)
{
vector< bool > prime(max_val + 1, true );
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= max_val; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= max_val; i += p)
prime[i] = false ;
}
}
int count = 0;
for ( int i = 0; i < s.length(); ++i) {
if (prime[ int (s[i])])
count++;
}
return count;
}
int main()
{
string S = "geeksforgeeks" ;
cout << PrimeCharacters(S);
return 0;
}
|
Java
class Solution
{
static final int max_val= 257 ;
static int PrimeCharacters(String s)
{
boolean prime[]= new boolean [max_val+ 1 ];
for ( int i= 0 ;i<=max_val;i++)
prime[i]= true ;
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= max_val; p++) {
if (prime[p] == true ) {
for ( int i = p * 2 ; i <= max_val; i += p)
prime[i] = false ;
}
}
int count = 0 ;
for ( int i = 0 ; i < s.length(); ++i) {
if (prime[( int )(s.charAt(i))])
count++;
}
return count;
}
public static void main(String args[])
{
String S = "geeksforgeeks" ;
System.out.print( PrimeCharacters(S));
}
}
|
Python3
from math import sqrt
max_val = 257
def PrimeCharacters(s) :
prime = [ True ] * (max_val + 1 )
prime[ 0 ] = False
prime[ 1 ] = False
for p in range ( 2 , int (sqrt(max_val)) + 1 ) :
if (prime[p] = = True ) :
for i in range ( 2 * p ,max_val + 1 , p) :
prime[i] = False
count = 0
for i in range ( len (s)) :
if (prime[ ord (s[i])]) :
count + = 1
return count
if __name__ = = "__main__" :
S = "geeksforgeeks" ;
print (PrimeCharacters(S))
|
C#
using System;
class GFG{
static readonly int max_val = 257;
static int PrimeCharacters(String s)
{
bool []prime = new bool [max_val + 1];
for ( int i = 0; i <= max_val; i++)
prime[i] = true ;
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= max_val; p++)
{
if (prime[p] == true )
{
for ( int i = p * 2; i <= max_val; i += p)
prime[i] = false ;
}
}
int count = 0;
for ( int i = 0; i < s.Length; ++i)
{
if (prime[( int )(s[i])])
count++;
}
return count;
}
public static void Main()
{
String S = "geeksforgeeks" ;
Console.Write( PrimeCharacters(S));
}
}
|
Javascript
<script>
const max_val = 257;
function PrimeCharacters(s) {
var prime = new Array(max_val + 1);
for ( var i = 0; i <= max_val; i++) prime[i] = true ;
prime[0] = false ;
prime[1] = false ;
for ( var p = 2; p * p <= max_val; p++) {
if (prime[p] === true ) {
for ( var i = p * 2; i <= max_val; i += p) prime[i] = false ;
}
}
var count = 0;
for ( var i = 0; i < s.length; ++i) {
if (prime[s[i].charCodeAt(0)]) count++;
}
return count;
}
var S = "geeksforgeeks" ;
document.write(PrimeCharacters(S));
</script>
|
Complexity Analysis:
- Time Complexity: O(max_val*log(log(max_val)))
- Auxiliary Space: O(max_val)
Last Updated :
08 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...