~mjorgensen/WordCounter

72aa508324c2b7a60bb538284f9dce5effb85ac4 — Matthew Jorgensen 11 months ago master
Created WordCounter program.
A  => .gitignore +41 -0
@@ 1,41 @@
# Autosave files
*~

# build
[Oo]bj/
[Bb]in/
packages/
TestResults/

# globs
Makefile.in
*.DS_Store
*.sln.cache
*.suo
*.cache
*.pidb
*.userprefs
*.usertasks
config.log
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.user
*.tar.gz
tarballs/
test-results/
Thumbs.db
.vs/

# Mac bundle stuff
*.dmg
*.app

# resharper
*_Resharper.*
*.Resharper

# dotCover
*.dotCover

A  => TestLibrary/TestLibrary.csproj +19 -0
@@ 1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\TextUtils\TextUtils.csproj" />
  </ItemGroup>
</Project>

A  => TestLibrary/TextUtilsTests.cs +29 -0
@@ 1,29 @@
using Xunit;
using TextUtils;
using System.Diagnostics;

namespace TestLibrary
{
    public class TextUtils_GetWordCountShould
    {
        [Fact]
        public void IgnoreCasing()
        {
            var wordCount = WordCount.GetWordCount("Jack", "Jack jack");

            Assert.Equal(2, wordCount);
        }

        [Theory]
        [InlineData(0, "Ting", "Does not appear in the string.")]
        [InlineData(1, "Ting", "Ting appears once.")]
        [InlineData(2, "Ting", "Ting appears twice with Ting.")]
        public void CountInstancesCorrectly(int count,
                                           string searchWord,
                                           string inputString)
        {
            Assert.Equal(count, WordCount.GetWordCount(searchWord,
                                                          inputString));
        }
    }
}
\ No newline at end of file

A  => TextUtils/TextUtils.csproj +7 -0
@@ 1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

A  => TextUtils/WordCount.cs +28 -0
@@ 1,28 @@
´╗┐using System;
using System.Linq;

namespace TextUtils
{
    public static class WordCount
    {
        public static int GetWordCount(string searchWord, string inputString)
        {
            // Null check these variables and determine if they have values.
            if (string.IsNullOrEmpty(searchWord) || string.IsNullOrEmpty(inputString)){
                return 0;
            }

            // Convert the string into an array of words.
            var source = inputString.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
                                           StringSplitOptions.RemoveEmptyEntries);

            // Create the query. Use ToLowerInvariant to match uppercase/lowercase strings.
            var matchQuery = from word in source
                             where word.ToLowerInvariant() == searchWord.ToLowerInvariant()
                             select word;

            // Count the matches, which executes the query. Return the result.
            return matchQuery.Count();
        }
    }
}

A  => WordCounter.sln +29 -0
@@ 1,29 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TextUtils", "TextUtils\TextUtils.csproj", "{0BD514D0-F09B-439F-8DC8-83538699ACD9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestLibrary", "TestLibrary\TestLibrary.csproj", "{754FE71D-5238-49BA-8555-18FB60594A17}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WordCounterApp", "WordCounterApp\WordCounterApp.csproj", "{EB68F711-AF7A-44E7-B8DD-3D374107E0BE}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{0BD514D0-F09B-439F-8DC8-83538699ACD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{0BD514D0-F09B-439F-8DC8-83538699ACD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{0BD514D0-F09B-439F-8DC8-83538699ACD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{0BD514D0-F09B-439F-8DC8-83538699ACD9}.Release|Any CPU.Build.0 = Release|Any CPU
		{754FE71D-5238-49BA-8555-18FB60594A17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{754FE71D-5238-49BA-8555-18FB60594A17}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{754FE71D-5238-49BA-8555-18FB60594A17}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{754FE71D-5238-49BA-8555-18FB60594A17}.Release|Any CPU.Build.0 = Release|Any CPU
		{EB68F711-AF7A-44E7-B8DD-3D374107E0BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{EB68F711-AF7A-44E7-B8DD-3D374107E0BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{EB68F711-AF7A-44E7-B8DD-3D374107E0BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{EB68F711-AF7A-44E7-B8DD-3D374107E0BE}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

A  => WordCounterApp/Program.cs +27 -0
@@ 1,27 @@
´╗┐using System;
using TextUtils;

namespace WordCounterApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a search word:");
            var searchWord = Console.ReadLine();
            Console.WriteLine("Provide a string to search:");
            var inputString = Console.ReadLine();

            var wordCount = WordCount.GetWordCount(searchWord, inputString);

            var pluralChar = "s";
            if (wordCount == 1)
            {
                pluralChar = string.Empty;
            }

            Console.WriteLine($"The search word {searchWord} appears " +
                              $"{wordCount} time{pluralChar}.");
        }
    }
}
\ No newline at end of file

A  => WordCounterApp/WordCounterApp.csproj +11 -0
@@ 1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\TextUtils\TextUtils.csproj" />
  </ItemGroup>
</Project>