input a series of numbers separated by commas, parse them into integers and output the sum

image_pdfimage_print

using System;

class Class1 {
public static void Main(string[] args) {
Console.WriteLine(“Input a series of numbers separated by commas:”);
string input = Console.ReadLine();
char[] cDividers = { ',', ' ' };
string[] segments = input.Split(cDividers);
int nSum = 0;
foreach (string s in segments) {
if (s.Length > 0) {
if (IsAllDigits(s)) {
int num = Int32.Parse(s);
Console.WriteLine(“Next number = {0}”, num);
nSum += num;
}
}
}
Console.WriteLine(“Sum = {0}”, nSum);

}

public static bool IsAllDigits(string sRaw) {
string s = sRaw.Trim();
if (s.Length == 0) {
return false;
}

for (int index = 0; index < s.Length; index++) { if (Char.IsDigit(s[index]) == false) { return false; } } return true; } } [/csharp]