Count characters in a string whose ASCII values are prime in C++

C++Server Side ProgrammingProgramming

We are given a string of any length containing both uppercase and lowercase letters and the task is to compute the count of those characters whose ASCII values are prime.

The ASCII values of uppercase letters[A-Z] start with 65 till 90 and lowercase letters[a-z] starts with 97 till 122.

For Example

Input string str = ‘Aebg’
Output count is: 2

Explanation − The ASCII value for A is 65 which is a non-prime number so it willn’t be counted, e is 101 which is a prime number so it will be counted, b is 66 which is a nonprime number so it willn’t be counted and g is 103 which is a prime number so it will be counted. Therefore, in total there are 2 characters having prime ASCII value.

Input − string str = ‘GOXFH’
Output − count is: 2

Explanation − The ASCII value for G is 71 which is a prime number so it will be counted, O is 79 which is a prime number so it will be counted, X is 88 which is a non-prime number so it willn’t be counted, F is 70 which isn’t a prime number so it willn’t be counted and H is 72 which isn’t a prime number so it willn’t be counted . Therefore, in total there are 2 characters having prime ASCII value.

Approach used in the below program is as follows

  • Input the string and store it in a variable let’s say str

  • Calculate the length of string str using length() function that will return an integer value as per the number of letters in the string including the spaces.

  • Declare a function to calculate the prime value which we will check against every letter determined

  • Traverse the loop starting from i to 0 till length of a string

  • Inside the loop, check if the ASCII value of a character traversed is prime or not. If it is a prime, then increase the count by 1 else don’t increment the value.

  • Return the total value of count

  • Print the result.

Example

 Live Demo

#include <iostream>
#include <vector>
using namespace std;
#define max_val 257
// Function to find prime characters in the string
int countprime(string str){
   // Using SIEVE for finding the prime numbers less
   // than Equal to 'max_val'
   // A Boolean array "prime[0..n]". A
   // value in prime[i] will finally be false
   // if i is Not a prime, else true.
   vector<bool> prime(max_val + 1, true);
   // 0 and 1 are not primes
   prime[0] = false;
   prime[1] = false;
   for (int p = 2; p * p <= max_val; p++){
      // If prime[p] is not changed, then
      // it is a prime
      if (prime[p] == true) {
         // Upfating the all multiples of p
         for (int i = p * 2; i <= max_val; i += p){
            prime[i] = false;
         }
      }
   }
   int result = 0;
   // traversing the whole string.
   for (int i = 0; i < str.length(); ++i){
      if (prime[int(str[i])]){
         result++;
      }
   }
   return result;
}
// main function
int main(){
   string str = "tutorialspoint";
   // print required answer
   cout <<"count is: "<< countprime(str);
   return 0;
}

Output

If we run the above code it will generate the following output −

count is:1
raja
Published on 15-May-2020 09:20:44

Advertisements