Expression Validator using Stack

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

namespace ExpressionValidator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the string");

string input = Console.ReadLine();

if (IsInputValid(input))
{
Console.WriteLine("Expression is valid");
}
else
{
Console.WriteLine("Expression is not valid");
}

Console.ReadLine();
}

static bool IsInputValid(string input)
{
Stack s = new Stack();

foreach (char ch in input)
{
if (ch == '(' || ch == '{')
{
s.Push(ch);
}
else
{
if (ch == ')' || ch == '}')
{
char poppedItem = s.Pop();
if ((ch == ')' && poppedItem == '(') || (ch == '}' && poppedItem == '{'))
{
continue;
}
else
{
return false;
}
}
}
}

if (s.Count != 0)
return false;
return true;
}
}
}

Comments

Popular posts from this blog

Network Security: LAN manager authentication level

Cisco AnyConnect Secure Mobility Client - VPN service not available. The VPN agent service is not responding