Advertisement
kolioi

25. Sum of Numbers in String C++

Dec 2nd, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4.  
  5. int SumNumbers(const std::string&);
  6.  
  7. int main()
  8. {
  9.     std::string s;
  10.     std::cin >> s;
  11.  
  12.     std::cout << SumNumbers(s) << std::endl;
  13.  
  14.     return 0;
  15. }
  16.  
  17. int SumNumbers(const std::string& s)
  18. {
  19.     int sum = 0;
  20.     for (char ch : s)
  21.         if (std::isdigit(ch))
  22.             sum += ch - '0';
  23.  
  24.     return sum;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement