Home > Backend Development > C++ > Find the sum of the ASCII values ​​of characters in prime positions

Find the sum of the ASCII values ​​of characters in prime positions

PHPz
Release: 2023-09-11 20:29:12
forward
643 people have browsed it

Find the sum of the ASCII values ​​of characters in prime positions

introduce

In this tutorial, we will learn the concept of C to find the sum of ASCII values ​​of characters appearing in primary positions. Prime positions are characters with positions 2, 3, 5, or any other prime number.

ASCII (American Standard Code for Information Interchange) values ​​are unique numerical values ​​for letters, letters, punctuation marks, and other characters used in encoding. It is used to communicate with computers because computers cannot understand human language.

There are 128 ASCII values, from 0 to 127. Uppercase and lowercase letters have different ASCII values.

We will develop a C code to calculate the ASCII value of a character located in a prime position. We use the length() function of the string class to store the length of the input string.

Example 1

There is an string = “Hello”
Sum of prime position ASCII value characters = 320
Copy after login
In the above string “Hello” the ASCII value of each character is 
H =  72
e = 101
l = 108
l = 108
o = 111
Copy after login

The characters in the prime position are "e", "l", and "o". Add the ASCII values ​​of the characters in these prime positions.

Sample 2

Input string = “abcd”
Sum = 197
Copy after login

The main 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
Copy after login

Calculate the sum of the ASCII values ​​of the characters at the primary position in the string.

grammar

  • sqrt() − This library function is defined in the math library and it returns the square root of the input number

sqrt(n)
Copy after login
  • length() − This string library function returns the length of the input string, where the length is the number of characters in the string.

string_name.length(); 
Copy after login

Example 1

We will use the C programming language to implement an example that calculates the sum of the ASCII values ​​of characters located at prime positions in the input string. The C code input string is "Hello". We first find all prime positions of the input string by applying the logic of identifying prime numbers. Use a for loop to find the ASCII values ​​of all characters of the input string. Sums the ASCII values ​​of input string characters 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;
}
Copy after login

Output

"Sum of ASCII values of characters at prime positions: 320
Copy after login

Example 2

is translated as:

Example 2

Here, we use different logic to implement this example. We first find the prime position, find these ASCII values ​​in the main() function 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;
}
Copy after login

Output

Sum of ASCII values at prime position: 665
Copy after login

in conclusion

In this tutorial, we developed C code to find the sum of the ASCII values ​​of characters at prime positions. We used the length() function of the String class to find the length of the parameter string. ASCII values ​​are predetermined values ​​of letters and other characters used to help computers communicate. In this tutorial, we implemented two examples with different logic and used some C library functions. The most important library function is length(). The length() library function returns the length of a string.

The above is the detailed content of Find the sum of the ASCII values ​​of characters in prime positions. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template