Sums of ASCII values of each word in a sentence
We are given a sentence of english language(can also contain digits), we need to compute and print the sum of ASCII values of characters of each word in that sentence.
Examples:
Input : GeeksforGeeks, a computer science portal for geeks
Output : Sentence representation as sum of ASCII each character in a word:
1361 97 879 730 658 327 527
Total sum -> 4579
Here, [GeeksforGeeks, ] -> 1361, [a] -> 97, [computer] -> 879, [science] -> 730
[portal] -> 658, [for] -> 327, [geeks] -> 527
Input : I am a geek
Output : Sum of ASCII values:
73 206 97 412
Total sum -> 788
Approach:
- Iterate over the length of the string and keep converting the characters to their ASCII
- Keep adding up the values till the end of sentence.
- When we come across a space character, we store the sum calculated for that word and set the sum equal to zero again.
- Later, we print elements
C++
// C++ implementation for representing // each word in a sentence as sum of // ASCII values of each word #include <iostream> #include <string> #include <vector> using namespace std; // Function to compute the sum of ASCII values of each // word separated by a space and return the total sum // of the ASCII values, excluding the space. long long int ASCIIWordSum(string str, vector<long long int>& sumArr) { int l = str.length(); int sum = 0; long long int bigSum = 0L; for (int i = 0; i < l; i++) { // Separate each word by // a space and store values // corresponding to each word if (str[i] == ' ') { bigSum += sum; sumArr.push_back(sum); sum = 0; } else // Implicit type casting sum += str[i]; } // Storing the value of last word sumArr.push_back(sum); bigSum += sum; return bigSum; } // Driver function int main() { string str = "GeeksforGeeks a computer science " "portal for Geeks"; vector<long long int> sumArr; // Calling function long long int sum = ASCIIWordSum(str, sumArr); cout << "Sum of ASCII values:" << std::endl; for (auto x : sumArr) cout << x << " "; cout << endl << "Total sum -> " << sum; return 0; } |
chevron_right
filter_none
Java
// Java program for representing // each word in a sentence as sum of // ASCII values of each word import java.util.*; import java.lang.*; class Rextester { // Function to compute the sum of ASCII values of // each word separated by a space and return the // total sum of the ASCII values, excluding the // space. static long ASCIIWordSum(String str, long sumArr[]) { int l = str.length(); int pos = 0; long sum = 0; long bigSum = 0; for (int i = 0; i < l; i++) { // Separate each word by // a space and store values // corresponding to each word if (str.charAt(i) == ' ') { bigSum += sum; sumArr[pos++] = sum; sum = 0; } else // Implicit type casting sum += str.charAt(i); } // Storing the sum of last word sumArr[pos] = sum; bigSum += sum; return bigSum; } // Driver function public static void main(String args[]) { String str = "GeeksforGeeks, a computer science portal for geeks"; // Counting the number of words in the input sentence int ctr = 0; for (int i = 0; i < str.length(); i++) if (str.charAt(i) == ' ') ctr++; long sumArr[] = new long[ctr + 1]; // Calling function long sum = ASCIIWordSum(str, sumArr); // Printing equivalent sum of the words in the // sentence System.out.println("Sum of ASCII values:"); for (int i = 0; i <= ctr; i++) System.out.print(sumArr[i] + " "); System.out.println(); System.out.print("Total sum -> " + sum); } } |
chevron_right
filter_none
Python 3
# Python 3 implementation for representing # each word in a sentence as sum of # ASCII values of each word # Function to compute the sum of ASCII # values of each word separated by a space # and return the total sum of the ASCII # values, excluding the space. def ASCIIWordSum(str, sumArr): l = len(str) sum = 0 bigSum = 0 for i in range(l): # Separate each word by a space # and store values corresponding # to each word if (str[i] == ' '): bigSum += sum sumArr.append(sum) sum = 0 else: # Implicit type casting sum += ord(str[i]) # Storing the value of last word sumArr.append(sum) bigSum += sum return bigSum # Driver Code if __name__ == "__main__": str = "GeeksforGeeks a computer science portal for Geeks" sumArr = [] # Calling function sum = ASCIIWordSum(str, sumArr) print("Sum of ASCII values:" ) for x in sumArr: print(x, end = " ") print() print("Total sum -> ", sum) # This code is contributed by ita_c |
chevron_right
filter_none
C#
// C# program for representing each // word in a sentence as sum of // ASCII values of each word using System; class GFG { // Function to compute the sum of ASCII // values of each word separated by a // space and return the total sum of // the ASCII values, excluding the space. static long ASCIIWordSum(String str, long []sumArr) { int l = str.Length; int pos = 0; long sum = 0; long bigSum = 0; for (int i = 0; i < l; i++) { // Separate each word by // a space and store values // corresponding to each word if (str[i] == ' ') { bigSum += sum; sumArr[pos++] = sum; sum = 0; } else // Implicit type casting sum += str[i]; } // Storing the sum of last word sumArr[pos] = sum; bigSum += sum; return bigSum; } // Driver function public static void Main() { String str = "GeeksforGeeks, a computer " + "science portal for geeks"; // Counting the number of words // in the input sentence int ctr = 0; for (int i = 0; i < str.Length; i++) if (str[i] == ' ') ctr++; long []sumArr = new long[ctr + 1]; // Calling function long sum = ASCIIWordSum(str, sumArr); // Printing equivalent sum of // the words in the sentence Console.WriteLine("Sum of ASCII values:"); for (int i = 0; i <= ctr; i++) Console.Write(sumArr[i] + " "); Console.WriteLine(); Console.Write("Total sum -> " + sum); } } // This code is contributed by nitin mittal |
chevron_right
filter_none
Output:
Sum of ASCII values: 1317 97 879 730 658 327 495 Total sum -> 4503
The complexity of the approach is ![]()
Recommended Posts:
- Print each word in a sentence with their corresponding average of ASCII values
- Print given sentence into its equivalent ASCII form
- Convert the ASCII value sentence to its equivalent string
- Program to replace a word with asterisks in a sentence
- Print longest palindrome word in a sentence
- Program for length of the longest word in a sentence
- Java program to count the characters in each word in a given sentence
- Convert a string to hexadecimal ASCII values
- Map function and Dictionary in Python to sum ASCII values
- Average of ASCII values of characters of a given string
- Check if a string contains only alphabets in Java using ASCII values
- Count characters in a string whose ASCII values are prime
- 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
- Find the sum of the ascii values of characters which are present at prime positions
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.


