Saturday, April 20, 2024

Find the sum of numbers in a string part 2

 using System;


namespace RetrieveData

{

    internal class Program2

    {



        static void Main(string[] args)

        {


            int addedValue = AddNumber("A1B3C5D6$623325");


            Console.WriteLine($"The sum of numbers in the given string  is {addedValue}");

            Console.ReadLine();

        }


        private static int AddNumber(string  v)

        {

            int num;

            int sum = 0;

            for (int i = 0; i < v.Length; i++)

            {


                 bool isNumber = int.TryParse(v.Substring(i,1), out  num);

                

                if (isNumber)

                {

                    sum += num;


                }

                                

            }

            return sum;

        }

    }

}


No comments:

Post a Comment