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
Implementation:
C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
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++) {
if (str[i] == ' ') {
bigSum += sum;
sumArr.push_back(sum);
sum = 0;
}
else
sum += str[i];
}
sumArr.push_back(sum);
bigSum += sum;
return bigSum;
}
int main()
{
string str = "GeeksforGeeks a computer science "
"portal for Geeks";
vector<long long int> sumArr;
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;
}
|
Java
import java.util.*;
import java.lang.*;
class Rextester {
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++) {
if (str.charAt(i) == ' ') {
bigSum += sum;
sumArr[pos++] = sum;
sum = 0;
}
else
sum += str.charAt(i);
}
sumArr[pos] = sum;
bigSum += sum;
return bigSum;
}
public static void main(String args[])
{
String str = "GeeksforGeeks, a computer science portal for geeks";
int ctr = 0;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == ' ')
ctr++;
long sumArr[] = new long[ctr + 1];
long sum = ASCIIWordSum(str, sumArr);
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);
}
}
|
Python 3
def ASCIIWordSum(str, sumArr):
l = len(str)
sum = 0
bigSum = 0
for i in range(l):
if (str[i] == ' '):
bigSum += sum
sumArr.append(sum)
sum = 0
else:
sum += ord(str[i])
sumArr.append(sum)
bigSum += sum
return bigSum
if __name__ == "__main__":
str = "GeeksforGeeks a computer science portal for Geeks"
sumArr = []
sum = ASCIIWordSum(str, sumArr)
print("Sum of ASCII values:" )
for x in sumArr:
print(x, end = " ")
print()
print("Total sum -> ", sum)
|
C#
using System;
class GFG {
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++) {
if (str[i] == ' ')
{
bigSum += sum;
sumArr[pos++] = sum;
sum = 0;
}
else
sum += str[i];
}
sumArr[pos] = sum;
bigSum += sum;
return bigSum;
}
public static void Main()
{
String str = "GeeksforGeeks, a computer " +
"science portal for geeks";
int ctr = 0;
for (int i = 0; i < str.Length; i++)
if (str[i] == ' ')
ctr++;
long []sumArr = new long[ctr + 1];
long sum = ASCIIWordSum(str, sumArr);
Console.WriteLine("Sum of ASCII values:");
for (int i = 0; i <= ctr; i++)
Console.Write(sumArr[i] + " ");
Console.WriteLine();
Console.Write("Total sum -> " + sum);
}
}
|
Javascript
<script>
function ASCIIWordSum(str,sumArr)
{
let l = str.length;
let pos = 0;
let sum = 0;
let bigSum = 0;
for (let i = 0; i < l; i++) {
if (str[i] == ' ') {
bigSum += sum;
sumArr[pos++] = sum;
sum = 0;
}
else
sum += str[i].charCodeAt(0);
}
sumArr[pos] = sum;
bigSum += sum;
return bigSum;
}
let str = "GeeksforGeeks, a computer
science portal for geeks";
let ctr = 0;
for (let i = 0; i < str.length; i++)
if (str[i] == ' ')
ctr++;
let sumArr = new Array(ctr + 1);
let sum = ASCIIWordSum(str, sumArr);
document.write("Sum of ASCII values:<br>");
for (let i = 0; i <= ctr; i++)
document.write(sumArr[i] + " ");
document.write("<br>");
document.write("Total sum -> " + sum);
</script>
|
OutputSum of ASCII values:
1317 97 879 730 658 327 495
Total sum -> 4503
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the length of the string that is number of characters in the sentence.
Auxiliary Space: O(W), as we are using extra space for the sumArr. Where W is the number of words in the sentence.
Last Updated :
08 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...