Find the sum of the ascii values of characters which are present at prime positions
Given a string str of size N, the task is to find the sum of all ASCII values of the characters which are present at prime positions.
Examples:
Input: str = “abcdef”
Output: 298
‘b’, ‘c’ and ‘e’ are the only characters which are
at prime positions i.e. 2, 3 and 5 respectively.
And sum of their ASCII values is 298.
Input: str = “geeksforgeeks”
Output: 644
Approach: An efficient approach is to traverse through the whole string and find if the particular position is prime or not. If the position of the current character is prime then add the ASCII value of the character to the answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true // if n is prime bool isPrime(int n) { if (n == 0 || n == 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } // Function to return the sum // of the ascii values of the characters // which are present at prime positions int sumAscii(string str, int n) { // To store the sum int sum = 0; // For every character for (int i = 0; i < n; i++) { // If current position is prime // then add the ASCII value of the // character at the current position if (isPrime(i + 1)) sum += (int)(str[i]); } return sum; } // Driver code int main() { string str = "geeksforgeeks"; int n = str.size(); cout << sumAscii(str, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true // if n is prime static boolean isPrime(int n) { if (n == 0 || n == 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } // Function to return the sum // of the ascii values of the characters // which are present at prime positions static int sumAscii(String str, int n) { // To store the sum int sum = 0; // For every character for (int i = 0; i < n; i++) { // If current position is prime // then add the ASCII value of the // character at the current position if (isPrime(i + 1)) { sum += (int) (str.charAt(i)); } } return sum; } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; int n = str.length(); System.out.println(sumAscii(str, n)); } } // This code contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach from math import sqrt # Function that returns true # if n is prime def isPrime(n) : if (n == 0 or n == 1) : return False; for i in range(2, int(sqrt(n)) + 1) : if (n % i == 0): return False; return True; # Function to return the sum # of the ascii values of the characters # which are present at prime positions def sumAscii(string, n) : # To store the sum sum = 0; # For every character for i in range(n) : # If current position is prime # then add the ASCII value of the # character at the current position if (isPrime(i + 1)) : sum += ord(string[i]); return sum; # Driver code if __name__ == "__main__" : string = "geeksforgeeks"; n = len(string); print(sumAscii(string, n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true // if n is prime static bool isPrime(int n) { if (n == 0 || n == 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } // Function to return the sum // of the ascii values of the characters // which are present at prime positions static int sumAscii(string str, int n) { // To store the sum int sum = 0; // For every character for (int i = 0; i < n; i++) { // If current position is prime // then add the ASCII value of the // character at the current position if (isPrime(i + 1)) { sum += (int) (str[i]); } } return sum; } // Driver code public static void Main() { string str = "geeksforgeeks"; int n = str.Length; Console.WriteLine(sumAscii(str, n)); } } // This code contributed by anuj_67.. |
644
Recommended Posts:
- Count characters in a string whose ASCII values are prime
- Program to find the product of ASCII values of characters in a string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Average of ASCII values of characters of a given string
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Program to print characters present at prime indexes in a given string
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Program to find the largest and smallest ASCII valued characters in a string
- Map function and Dictionary in Python to sum ASCII values
- Convert a string to hexadecimal ASCII values
- Sums of ASCII values of each word in a sentence
- Check if a string contains only alphabets in Java using ASCII values
- Print each word in a sentence with their corresponding average of ASCII values
- Count the number of words having sum of ASCII values less than and greater than k
- Minimize ASCII values sum after removing all occurrences of one character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


