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 letter to the string and add its ASCII value to the sum.
- If there is an empty 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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Nov, 2022
Like Article
Save Article