Ad

Você recebeu 2 nomes.

Verifique se a soma das letras do 1º nome é igual à soma das letras do 2º nome. Se o nome ou sobrenome for nulo, produza NULL.

Por exemplo:

"Ana" e "Nana" "Ana" = 65+110+110+97 = 382 "Nana" = 78+97+110+97 = 382 Resultado: "Anna" e "Nana" => true

"Sebastião" e "Patrícia" => false

"João" e nulo => "NULO"

Code
Diff
  • #include <string>
    #include <numeric>
    #include <algorithm>
    
    class StringComparer {
    public:
        static bool verifySum(const std::string& w1, const std::string& w2) {
            int sumW1 = sumOfCharacters(w1);
            int sumW2 = sumOfCharacters(w2);
    
            return sumW1 == sumW2;
        }
      
        static int sumOfCharacters(const std::string& word) {
            return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
                return sum + static_cast<int>(ch);
            });
        }
    };
    • #include <string>
    • #include <numeric>
    • #include <algorithm>
    • class StringComparer {
    • public:
    • static bool verifySum(const std::string& w1, const std::string& w2) {
    • int sumW1 = sumOfCharacters(w1);
    • int sumW2 = sumOfCharacters(w2);
    • std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;
    • std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;
    • return sumW1 == sumW2;
    • }
    • static int sumOfCharacters(const std::string& word) {
    • return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
    • return sum + static_cast<int>(ch);
    • });
    • }
    • };

You have received 2 names.

Verify if the sum of letters of the 1 name is the same as sum of the letters of the 2 name. If the name or surname is null output NULL.

For example:

"Anna" and "Nana"
"Anna" = 65+110+110+97 = 382
"Nana" = 78+97+110+97 = 382
Result: "Anna" and "Nana" => "TRUE"

"Sebastian" and "Patricia" => "FALSE"

"John" and null => "NULL"

Code
Diff
  • #include <string>
    #include <numeric>
    #include <algorithm>
    
    class StringComparer {
    public:
        static bool verifySum(const std::string& w1, const std::string& w2) {
            int sumW1 = sumOfCharacters(w1);
            int sumW2 = sumOfCharacters(w2);
    
            return sumW1 == sumW2;
        }
      
        static int sumOfCharacters(const std::string& word) {
            return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
                return sum + static_cast<int>(ch);
            });
        }
    };
    • #include <string>
    • #include <numeric>
    • #include <algorithm>
    • class StringComparer {
    • public:
    • static bool verifySum(const std::string& w1, const std::string& w2) {
    • int sumW1 = sumOfCharacters(w1);
    • int sumW2 = sumOfCharacters(w2);
    • std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;
    • std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;
    • return sumW1 == sumW2;
    • }
    • static int sumOfCharacters(const std::string& word) {
    • return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
    • return sum + static_cast<int>(ch);
    • });
    • }
    • };

Você recebeu 2 nomes.

Verifique se a soma das letras do 1º nome é igual à soma das letras do 2º nome. Se o nome ou sobrenome for nulo, produza NULL.

Por exemplo:

"Ana" e "Nana"
"Ana" = 65+110+110+97 = 382
"Nana" = 78+97+110+97 = 382
Resultado: "Anna" e "Nana" => true

"Sebastião" e "Patrícia" => false

"João" e nulo => "NULO"

Code
Diff
  • #include <string>
    #include <numeric>
    #include <algorithm>
    
    class StringComparer {
    public:
        static bool verifySum(const std::string& w1, const std::string& w2) {
            int sumW1 = sumOfCharacters(w1);
            int sumW2 = sumOfCharacters(w2);
    
            std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;
            std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;
    
            return sumW1 == sumW2;
        }
      
        static int sumOfCharacters(const std::string& word) {
            return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
                return sum + static_cast<int>(ch);
            });
        }
    };
    • def verifySum(w1, w2)
    • return false if w1.nil? || w2.nil?
    • w1.downcase().split('').map(&:ord).inject(:+) == w2.downcase().split('').map(&:ord).inject(:+)
    • end
    • #include <string>
    • #include <numeric>
    • #include <algorithm>
    • class StringComparer {
    • public:
    • static bool verifySum(const std::string& w1, const std::string& w2) {
    • int sumW1 = sumOfCharacters(w1);
    • int sumW2 = sumOfCharacters(w2);
    • std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;
    • std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;
    • return sumW1 == sumW2;
    • }
    • static int sumOfCharacters(const std::string& word) {
    • return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
    • return sum + static_cast<int>(ch);
    • });
    • }
    • };