Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class ReadNumbersFromString
- {
- static void Main()
- {
- string str = Console.ReadLine();//пр: {12 0 3 45}
- //обхождаме стринга за да преброим ' '
- int count = 0;
- foreach (char c in str)
- if (c == ' ') count++;
- // разделяме стринга при празните места и го записваме като масив
- string[] numAsStr = str.Split(' ');
- int sum = 0;
- //обхождаме масива,като прибавяме към сумата елементитие му ,които парсваме
- for (int i = 0; i < count + 1; i++)
- {
- int num = int.Parse(numAsStr[i]);
- sum += num;
- }
- Console.WriteLine(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment