0

When I'm putting the following code specifically in the immediate window in Visual studio, it returns correctly:

whatToMatch.Remove((whatToMatch.IndexOf(input[i])), 1)

But when I put it in a program as shown below, it fails:-

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

namespace IsPangram
{
    class Program
    {
        static void Main(string[] args)
        {
            string whatToMatch = "abcdefghijklmnopqrstuvwxyz";
            string input = Console.ReadLine().ToLower();
            for (int i = 0; i < input.Length; i++)
            {
                if (whatToMatch.Contains(input[i]))
                {
                    whatToMatch.Remove((whatToMatch.IndexOf(input[i])), 1);
                }
                if (whatToMatch.Length == 0)
                    Console.WriteLine("pangram");
            }
            Console.WriteLine("not pangram");

        }
    }
}

I was expecting "whatToMatch" to change dynamically as it is correct code, but it's not changing. Why? And how to resolve this issue?

5
  • 3
    Break point, add pepper and salt to taste, shake and repeat Commented Feb 28, 2018 at 8:02
  • And for your enjoyment msdn.microsoft.com/en-us/library/5557y8b4.aspx Commented Feb 28, 2018 at 8:03
  • 7
    String is immutable, all string.remove() does is return a new string, it doesn't modify the one you call it on, so use whatToMatch = whatToMatch.Remove((whatToMatch.IndexOf(input[i])), 1) instead Commented Feb 28, 2018 at 8:05
  • Thank you. I will try using a stringBuilder or use your strategy. Commented Feb 28, 2018 at 8:09
  • @AlexanderDerck, convert the comment to answer if possible Commented Feb 28, 2018 at 8:15

2 Answers 2

1

From msdn about String.Remove Method (Int32, Int32)

It returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

So it doesn't modify the string that call it, it return a new string. So you should use

whatToMatch = whatToMatch.Remove((whatToMatch.IndexOf(input[i])), 1)
Sign up to request clarification or add additional context in comments.

Comments

0

As already mentioned, strings in .NET are immutable, therefore you cannot expect your string to change dynamically.

Here is a concise solution to your problem using LINQ:

using System;
using System.Collections.Generic;
using System.Linq;

namespace IsPangram
{
    static class Program
    {
        public static bool IsPangram(this string input)
        {
            return
                !input.ToLower()
                      .Aggregate("abcdefghijklmnopqrstuvwxyz".ToList(),
                                 (ts, c) => ts.Where(x => x != c).ToList())
                      .Any();
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(Console.ReadLine().IsPangram() ?
                              "Is pangram" :
                              "Is not pangram");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.