Answer to Question #273689 in C# for Holik

Question #273689

10. The text is given. Determine the sum of the numbers in the text.

1
Expert's answer
2021-12-04T12:13:01-0500
using System;

namespace Test
{
    class NumbersTest
    {
        static int Main()
        {
            string str = Console.ReadLine();
            string buffer = "";

            foreach (char c in str)
            {
                buffer += (Char.IsNumber(c) || c == '-') ? c : ' ';
            }

            string[] arr = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            int sum = 0;

            foreach (string s in arr)
            {
                sum += int.Parse(s);
            }

            Console.WriteLine("The sum of the numbers is {0}", sum);

            return 0;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS