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.
Input: sentence = "Learning a string algorithm" Output: Learning - 102 a - 97 string - 110 algorithm - 107
- 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:
Learning - 102 a - 97 string - 110 algorithm - 107
Time Complexity: O(N)
- Sums of ASCII values of each word in a sentence
- Average of ASCII values of characters of a given string
- Print given sentence into its equivalent ASCII form
- Print longest palindrome word in a sentence
- Convert the ASCII value sentence to its equivalent string
- Program for length of the longest word in a sentence
- Program to replace a word with asterisks in a sentence
- Java program to count the characters in each word in a given sentence
- Convert a string to hexadecimal ASCII values
- Map function and Dictionary in Python to sum ASCII values
- Count the number of words having sum of ASCII values less than and greater than k
- Count characters in a string whose ASCII values are prime
- Check if a string contains only alphabets in Java using ASCII values
- Minimize ASCII values sum after removing all occurrences of one character
- Program to find the product of ASCII values of characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.