
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of ASCII Values at Prime Positions
Introduction
In this tutorial, we will learn the concept of c++ to find the sum of ASCII values of the characters present in the prime position. Prime position means characters whose position is 2, 3, 5, or any other prime number.
ASCII (American Standard Code for Information Interchange) values are unique numerical values for alphabets, letters, punctuation marks, and other characters used in coding. It is used for communication with computers as the computer does not understand human language.
There are 128 ASCII values starting from 0 to 127. Capital and small alphabets have separate ASCII values.
We will develop a C++ code to calculate the ASCII values of characters located at the prime position. We used the length() function of the string class to store the length of the input string.
Sample 1
There is an string = "Hello" Sum of prime position ASCII value characters = 320
In the above string "Hello" the ASCII value of each character is H = 72 e = 101 l = 108 l = 108 o = 111
The characters at prime position are "e", "l", "o". Add the ASCII values of these prime position characters.
Sample 2
Input string = "abcd" Sum = 197
The prime position characters in the input string "abcd" are "b", and "c".
The ASCII value of input string characters is as listed: a = 97 b = 98 c = 99 d = 100
Calculate the sum of ASCII values of characters lying at the prime position in the string.
Syntax
sqrt() ? This library function is defined in the math library and it returns the square root of the input number
sqrt(n)
length() ? This string class library function returns the length of the input string, the length is the number of characters in a string.
string_name.length();
Example 1
We will implement an example using the C++ programming language to calculate the sum of the ASCII values of the characters located at the prime position in the input string. The C++ code input string is "Hello". We first find all the prime positions of the input string by applying the logic of identifying the prime number. Use a for loop to find the ASCII values of all the characters of the input string. Sum the ASCII values of the input string characters that lie at prime positions.
#include <iostream> #include <string> #include <vector> using namespace std; // User-defined function to find the prime positions bool find_prime(int p){ if (p <= 1) { return false; } // loop to check the prime number for (int x = 2; x*x <= p; x++) { if (p % x == 0) { return false; } } return true; } // user-defined function to sum ASCII values of the prime position characters int sum_of_primes(string st) { int sum1 = 0; //variable to store the input string length. int p = st.length(); vector<bool> primes(p+1, false); primes[2] = true; for (int x = 3; x <= p; x += 2) { primes[x] = find_prime(x); } for (int x = 2; x <= p; x++){ if (primes[x] && x <= p){ sum1 += int(st[x-1]); } } return sum1; } // controlling code int main(){ string st = "Hello"; int sum1 = sum_of_primes(st); cout << "Sum of ASCII values of characters at prime positions: " << sum1 << endl; return 0; }
Output
"Sum of ASCII values of characters at prime positions: 320
Example 2
Here, we are using a different logic to implement the example. We first find the prime positions and in the main() we find those ASCII values and add them.
#include <iostream> #include <cmath> using namespace std; bool isNumPrime(int n) { if (n < 2) return false; for (int x = 2; x <= sqrt(n); ++x) { if (n % x == 0) return false; } return true; } int main() { string str = "tutorialspoint"; //std::cout << "Enter a string: "; int s = 0; for (size_t x = 0; x < str.length(); ++x) { if (isNumPrime(x + 1)) { s += static_cast<int>(str[x]); } } cout << "Sum of ASCII values at prime positions: " << s << endl; return 0; }
Output
Sum of ASCII values at prime position: 665
Conclusion
In this tutorial, we develop C++ code to find the sum of the ASCII values of the prime position characters. We used the length() function of the string class to find the length of the parameter string. ASCII values are predetermined values of the alphabet and other characters to help computers communicate. In this tutorial, we implemented two examples with different logics and used some C++ library functions. The most important library function used is length(). This length() library function returns the length of the string.