Coding Exercise Dujour ~ manually removing characters from string & log scanning for counts

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpConsole
{
public static class Program
{
static void Main(string[] args)
{
Console.Write(removeCharacterFromString(“Thanks!!! for the #@somevalue eg. $100 (dollars) \n”, (char)’$’));
Console.Write(removeCharacterFromString(“Thanks!!! for the #@somevalue $100 (dollars) \n”));
Console.Write(“Here are the current counts \n”);
Console.Write(countsFromLOG(@”c:\tcounts.txt”));

int[] dasnumbers = { 20, 31, 5, 16, 22, 7 };
NumberLessThanInArray(20, dasnumbers);

Console.ReadKey();
}
private static readonly HashSet badChars =
new HashSet { ‘!’, ‘@’, ‘#’, ‘$’, ‘%’, ‘_’ };

public static string removeCharacterFromString(this string str)
{
var result = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++)
{
if (!badChars.Contains(str[i]))
result.Append(str[i]);
}
return result.ToString();
}

public static string removeCharacterFromString(this string str, char BadChar)
{
var result = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++)
{
if (BadChar != ((char)str[i]))
result.Append(str[i]);
}
return result.ToString();
}

public static long countsFromLOG(string fileFullname)
{
long SumTotal = 0;
try
{
StreamReader sr = new StreamReader(fileFullname);

int rowCounter = 0;

while (!sr.EndOfStream)
{
string currentLine = sr.ReadLine();

long lResult = 0;
if (long.TryParse(currentLine, out lResult))
{
SumTotal += System.Convert.ToInt64(currentLine);
}
rowCounter += 1;
}

return SumTotal;
}
catch (Exception ex)
{
dynamic msg = “Exception parsing log file ” + fileFullname + “. ” + ex.Message;
throw new Exception(msg, ex);
}
}

public static void NumberLessThanInArray(int Target, int[] numbers)
{
Console.Write(“\n Here are the numbers less than ” + Target + “\n”);
IEnumerable numbersSmallerThan = numbers.Where(r => r
{
Console.Write(i + ” “);
}
);

}

}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s