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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Check if a word is present in a sentence
- Find the word with most anagrams in a given sentence
- 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
- Minimize ASCII values sum after removing all occurrences of one character
- Program to find the XOR of ASCII values of characters in a string
- Check if a string contains only alphabets in Java using 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
- Program to find the product of ASCII values of characters in a string
- Find the sum of the ascii values of characters which are present at prime positions
- Split a given string into substrings of length K with equal sum of ASCII values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.