How to sum the ASCII values of each word in a sentence in C++

1 Answer

0 votes
#include <iostream>
#include <vector>

int sumASCII(std::string s) {
   int len = s.length();
   int sum = 0;
   
   for (int i = 0; i < len; i++) {
      if (s[i] != ' ') {
         sum += s[i];
      }
   }

   return sum;
}
int main () {
    std::string s = "c++ pro"; // 99 + 43 + 43 = 185 // 112 + 114 + 111 = 337 // 185 + 337 = 522

    int sum = sumASCII(s);

    std::cout << sum;
   
    return 0;
}



/*
run:

522

*/

 



answered Jun 30, 2020 by avibootz

Related questions

...