
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count characters in a string whose ASCII values are prime in C++
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
#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
- Related Articles
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k in C++
- Average of ASCII values of characters of a given string?
- Convert a string to hexadecimal ASCII values in C++
- Count of alphabets whose ASCII values can be formed with the digits of N in C++
- Python program to find the sum of Characters ascii values in String List
- C program to print the ASCII values in a string.
- XOR of Prime Frequencies of Characters in a String in C++
- Count numbers from range whose prime factors are only 2 and 3 in C++
- Program to find the largest and smallest ASCII valued characters in a string in C++
- Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters
- Replacing all special characters with their ASCII value in a string - JavaScript
- Count Unique Characters of All Substrings of a Given String in C++
- Finding count of special characters in a string in JavaScript
- C# program to count upper and lower case characters in a given string
- Check whether the frequencies of all the characters in a string are prime or not in Python
