- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sums of ASCII values of each word in a sentence in c programming
The ASCII value of the ward is the integer presentation based on ASCII standards. In this problem, we are given a sentence and we have to calculate the sum of ASCII values of each word in the sentence.
For this we will have to find the ASCII values of all the characters of the sentence and then add them up, this will give the sum of ASCII values of letters in this word. we have to do the same for all words and finally, we will add all the sums and give a final sum of ASCII values of each word of the sentence.
for example
the sentence is “I love tutorials point”.
Output will be
105 438 999 554 2096
Example
#include <iostream> #include <string> #include <vector> using namespace std; long long int sumcalc (string str, vector < long long int >&arrsum) { int l = str.length (); int sum = 0; long long int bigSum = 0L; for (int i = 0; i < l; i++) { if (str[i] == ' ') { bigSum += sum; arrsum.push_back (sum); sum = 0; } else sum += str[i]; } arrsum.push_back (sum); bigSum += sum; return bigSum; } int main () { string str = "i love tutorials point"; vector < long long int >arrsum; cout<< "The string is "<<str<<endl; long long int sum = sumcalc (str, arrsum); cout << "Sum of ASCII values: "; for (auto x:arrsum) cout << x << " "; cout << endl << "Total sum -> " << sum; return 0; }
Output
The string is i love tutorials point Sum of ASCII values: 105 438 999 554 Total sum -> 2096
- Related Articles
- Python program to reverse each word in a sentence?
- Java program to reverse each word in a sentence
- C++ program for length of the longest word in a sentence
- Java program to count the characters in each word in a given sentence
- Print longest palindrome word in a sentence in C Program
- Finding the length of second last word in a sentence in JavaScript
- Convert the ASCII value sentence to its equivalent string in C++
- Find frequency of each word in a string in C#
- PHP program to find the first word of a sentence
- Print first letter of each word in a string in C#
- Convert a string to hexadecimal ASCII values in C++
- Write a C program to calculate the average word length of a sentence using while loop
- C program to print the ASCII values in a string.
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence

Advertisements