I am trying to test a simple console app which takes an input and checks if it has unique chars in it or not. On providing an input of " a", it just takes in the string as "a" and ignores the preceding spaces.
Can you please help me understand why this is happening and how I can make it accept the spaces as part of the string.
using System;
using System.Collections.Generic;
namespace CrackingTheCodingInterView
{
public class CheckUniqueChars
{
public static void Main()
{
string inputString;
bool checkUnique = false;
Console.WriteLine("Enter string to check for unique chars: ");
inputString = Console.ReadLine();
checkUnique = UniqueChars(inputString);
Console.WriteLine("String is: {0}", inputString);
string output = checkUnique ? "has" : "does not have";
Console.WriteLine("The input string {0} unique chars", output);
}
public static bool UniqueChars(string inputString)
{
List<char> uniqueCharsList = new List<char>();
foreach(char c in inputString)
{
if(uniqueCharsList.Contains(c))
{
return false;
}
else
{
uniqueCharsList.Add(c);
}
}
return true;
}
}
}
Thanks!