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?
string.remove()does is return a new string, it doesn't modify the one you call it on, so usewhatToMatch = whatToMatch.Remove((whatToMatch.IndexOf(input[i])), 1)instead