Advertisement
stak441

Classes and Objects - 05 - ValueFromString

Jan 23rd, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _06.ValuesFromString
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string stringNum = "43 68 9 23 318";
  13.             int breakIndex = -1;
  14.             int sum = 0;
  15.            
  16.             for (int i = 0; i < stringNum.Length; i++)
  17.             {
  18.                 if ((int)stringNum[i] == 32 || i == stringNum.Length-1)            //32 is the asci code for empty space
  19.                 {
  20.                     string tempNum = "0";
  21.                     if (i == stringNum.Length-1 && stringNum[i] != ' ')
  22.                     {
  23.                          tempNum = stringNum.Substring(breakIndex + 1, i - (breakIndex));    //length is 1 more, to include the last char
  24.                     }
  25.                     else
  26.                     {
  27.                         tempNum = stringNum.Substring(breakIndex + 1, i - (breakIndex + 1));
  28.                     }
  29.                    
  30.                     sum += int.Parse(tempNum);
  31.                     breakIndex = i;
  32.                 }
  33.  
  34.             }
  35.  
  36.             Console.WriteLine(sum);
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement