原文:

给定一个句子,任务是找出句子中每个单词的 ascii 值的平均值,并与单词一起打印出来。 例:

input: sentence = "learning a string algorithm"
output: 
learning - 102
a - 97
string - 110
algorithm - 107

进场:

  1. 取一个空字符串,开始一个字母一个字母地遍历句子。
  2. 将字母添加到字符串中,并将其 ascii 值添加到总和中。
  3. 如果有空格,用总和除以字符串(单词)的长度来计算平均值
  4. 清除字符串,以便它可以用于下一个单词
  5. 也将总和设置为零。

以下是上述方法的实现:

c

// c   program of the above approach
#include 
using namespace std;
// function to calculate the
// average of each word in a sentence
void calculateaverage(string sentence)
{
    // word initialised to an empty string
    string word = "";
    // sum of ascii values
    int sum = 0;
    int len = sentence.length();
    for (int i = 0; i < len;   i) {
        // if character is a space
        if (sentence[i] == ' ') {
            // calculate the average
            int average = sum / word.length();
            cout << word << " - "
                 << average << endl;
            // clear the word and
            // set the sum to 0
            word.clear();
            sum = 0;
        }
        else {
            // add the ascii value to sum and
            // also add the letter to the word
            sum  = sentence[i];
            word  = sentence[i];
        }
    }
    // calculate the average of last word
    int average = sum / word.length();
    cout << word << " - " << average;
}
// driver code
int main()
{
    // get the sentence
    string sentence
            = "learning a string algorithm";
    // calculate the average
    calculateaverage(sentence);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program of the above approach
import java.util.*;
public class solution
{
// function to calculate the
// average of each word in a sentence
static void calculateaverage(string sentence)
{
    // word initialised to an empty string
    string word = "";
    // sum of ascii values
    int sum = 0;
    int len = sentence.length();
    for (int i = 0; i < len;   i) {
        // if character is a space
        if (sentence.charat(i) == ' ') {
            // calculate the average
            int average = sum / word.length();
            system.out.println( word   " - "  average );
            // clear the word and
            // set the sum to 0
            word="";
            sum = 0;
        }
        else {
            // add the ascii value to sum and
            // also add the letter to the word
            sum  = sentence.charat(i);
            word  = sentence.charat(i);
        }
    }
    // calculate the average of last word
    int average = sum / word.length();
system.out.print( word   " - "   average);
}
// driver code
public static void main(string[] args)
{
    // get the sentence
    string sentence
            = "learning a string algorithm";
    // calculate the average
    calculateaverage(sentence);
}
}
//contributed by arnab kundu

python 3

# python 3 program of the above approach
# function to calculate the
# average of each word in a sentence
def calculateaverage(sentence):
    # word initialised to
    # an empty string
    word = ""
    # sum of ascii values
    sum = 0
    l = len(sentence)
    for i in range(l):
        # if character is a space
        if (sentence[i] == ' ') :
            # calculate the average
            average = sum // len(word)
            print(word , " - ", average)
            # clear the word and
            # set the sum to 0
            word = ""
            sum = 0
        else :
            # add the ascii value to sum and
            # also add the letter to the word
            sum  = ord(sentence[i])
            word  = sentence[i]
    # calculate the average of last word
    average = sum // len(word)
    print(word , " - " , average)
# driver code
if __name__ == "__main__":
    # get the sentence
    sentence = "learning a string algorithm"
    # calculate the average
    calculateaverage(sentence)
# this code is contributed
# by chitranayal

c

// c# implementation of above approach
using system;
class gfg
{
// function to calculate the
// average of each word in a sentence
static void calculateaverage(string sentence)
{
    // word initialised to an empty string
    string word = "";
    // sum of ascii values
    int sum = 0;
    int len = sentence.length;
    int average = 0;
    for (int i = 0; i < len;   i)
    {
        // if character is a space
        if (sentence[i] == ' ')
        {
            // calculate the average
            average = sum / word.length;
            console.writeline(word   " - "   average);
            // clear the word and
            // set the sum to 0
            word="";
            sum = 0;
        }
        else
        {
            // add the ascii value to sum and
            // also add the letter to the word
            sum  = sentence[i];
            word  = sentence[i];
        }
    }
    // calculate the average of last word
    average = sum / word.length;
    console.write(word   " - "   average);
}
// driver code
public static void main()
{
    // get the sentence
    string sentence = "learning a string algorithm";
    // calculate the average
    calculateaverage(sentence);
}
}
// this code is contributed
// by princiraj1992

java 描述语言


output: 

learning - 102
a - 97
string - 110
algorithm - 107

时间复杂度 : o(n)