0

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!

Ankit
  • 115
  • 10

1 Answers1

1

This is not an answer, but its working fine and not ignoring spaces

enter image description here

Sufyan Jabr
  • 791
  • 4
  • 20
  • Thanks Sufyan. Yes, Looks like it's an issue with the online editor I was coding in. Weird. https://dotnetfiddle.net/4eAyrn – Ankit Jul 10 '16 at 06:04
  • Most likely its some kind of validation or clean up they do before you submit the code – Sufyan Jabr Jul 10 '16 at 06:07
  • Yup, looks like it. I'll switch to using VS and Resharper as @SimpleVar suggested. – Ankit Jul 10 '16 at 06:10