Print each word in a sentence with their corresponding average of ASCII values
Last Updated :
08 Feb, 2024
Given a sentence, the task is to find the average of ASCII values of each word in the sentence and print it with the word.
Examples:
Input: sentence = “Learning a string algorithm”
Output:
Learning – 102
a – 97
string – 110
algorithm – 107
Approach:
- Take an empty string and start traversing the sentence letter by letter.
- Add a letter to the string and add its ASCII value to the sum.
- If there is a space calculate the average by dividing the sum by the length of the string (word)
- Clear the string so that it can be used for the next word
- Also, set the sum to zero.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void calculateAverage(string sentence)
{
string word = "" ;
int sum = 0;
int len = sentence.length();
for ( int i = 0; i < len; ++i) {
if (sentence[i] == ' ' ) {
int average = sum / word.length();
cout << word << " - "
<< average << endl;
word.clear();
sum = 0;
}
else {
sum += sentence[i];
word += sentence[i];
}
}
int average = sum / word.length();
cout << word << " - " << average;
}
int main()
{
string sentence
= "Learning a string algorithm" ;
calculateAverage(sentence);
return 0;
}
|
Java
import java.util.*;
public class Solution
{
static void calculateAverage(String sentence)
{
String word = "" ;
int sum = 0 ;
int len = sentence.length();
for ( int i = 0 ; i < len; ++i) {
if (sentence.charAt(i) == ' ' ) {
int average = sum / word.length();
System.out.println( word + " - " + average );
word= "" ;
sum = 0 ;
}
else {
sum += sentence.charAt(i);
word += sentence.charAt(i);
}
}
int average = sum / word.length();
System.out.print( word + " - " + average);
}
public static void main(String[] args)
{
String sentence
= "Learning a string algorithm" ;
calculateAverage(sentence);
}
}
|
Python 3
def calculateAverage(sentence):
word = ""
sum = 0
l = len (sentence)
for i in range (l):
if (sentence[i] = = ' ' ) :
average = sum / / len (word)
print (word , " - " , average)
word = ""
sum = 0
else :
sum + = ord (sentence[i])
word + = sentence[i]
average = sum / / len (word)
print (word , " - " , average)
if __name__ = = "__main__" :
sentence = "Learning a string algorithm"
calculateAverage(sentence)
|
C#
using System;
class GFG
{
static void calculateAverage(String sentence)
{
String word = "" ;
int sum = 0;
int len = sentence.Length;
int average = 0;
for ( int i = 0; i < len; ++i)
{
if (sentence[i] == ' ' )
{
average = sum / word.Length;
Console.WriteLine(word + " - " + average);
word= "" ;
sum = 0;
}
else
{
sum += sentence[i];
word += sentence[i];
}
}
average = sum / word.Length;
Console.Write(word + " - " + average);
}
public static void Main()
{
String sentence = "Learning a string algorithm" ;
calculateAverage(sentence);
}
}
|
Javascript
<script>
function calculateAverage(sentence) {
var word = "" ;
var sum = 0;
var len = sentence.length;
var average = 0;
for ( var i = 0; i < len; ++i) {
if (sentence[i] === " " ) {
average = parseInt(sum / word.length);
document.write(word + " - " + average + "<br>" );
word = "" ;
sum = 0;
} else {
sum += sentence[i].charCodeAt(0);
word += sentence[i];
}
}
average = parseInt(sum / word.length);
document.write(word + " - " + average + "<br>" );
}
var sentence = "Learning a string algorithm" ;
calculateAverage(sentence);
</script>
|
Output
Learning - 102
a - 97
string - 110
algorithm - 107
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...