Sumas de valores ASCII de cada palabra en una oración

Nos dan una oración en idioma inglés (también puede contener dígitos), necesitamos calcular e imprimir la suma de los valores ASCII de los caracteres de cada palabra en esa oración.

Ejemplos: 

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;
}

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);
    }
}

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

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

Javascript

<script>
 
// Javascript program 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.
    function ASCIIWordSum(str,sumArr)
    {
        let l = str.length;
        let pos = 0;
        let sum = 0;
        let bigSum = 0;
        for (let 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].charCodeAt(0);           
        }
   
        // Storing the sum of last word
        sumArr[pos] = sum;
        bigSum += sum;
        return bigSum;
    }
     
    // Driver function
     
    let str = "GeeksforGeeks, a computer
    science portal for geeks";
   
        // Counting the number of words
        // in the input sentence
        let ctr = 0;
        for (let i = 0; i < str.length; i++)
            if (str[i] == ' ')
                ctr++;
           
        let sumArr = new Array(ctr + 1);
   
        // Calling function
        let sum = ASCIIWordSum(str, sumArr);
   
        // Printing equivalent sum of the words in the
        // sentence
        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);
     
     
    // This code is contributed by avanitrachhadiya2155
     
</script>

Publicación traducida automáticamente

Artículo escrito por parthendo y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *