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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- 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
- Map function and Dictionary in Python to sum ASCII values
- Convert a string to hexadecimal ASCII values
- 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
- Minimize ASCII values sum after removing all occurrences of one character
- Count characters in a string whose ASCII values are prime
- Count the number of words having sum of ASCII values less than and greater than k
- Split a given string into substrings of length K with equal sum of ASCII values
- Find the sum of the ascii values of characters which are present at prime positions
- Program to find the product of ASCII values of characters in a string
- Minimum, maximum and average price values for all the items of given type
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.