Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Numerics;
- namespace SoftuniExercisesWithVariables
- {
- class Program
- {
- static bool isWellFormatted(string line)
- {
- Stack<char> lastOpen = new Stack<char>();
- foreach (var c in line)
- {
- switch (c)
- {
- case ')':
- if (lastOpen.Count == 0 || lastOpen.Pop() != '(') return false;
- break;
- case ']':
- if (lastOpen.Count == 0 || lastOpen.Pop() != '[') return false;
- break;
- case '}':
- if (lastOpen.Count == 0 || lastOpen.Pop() != '{') return false;
- break;
- case '(': lastOpen.Push(c); break;
- case '[': lastOpen.Push(c); break;
- case '{': lastOpen.Push(c); break;
- }
- }
- if (lastOpen.Count == 0) return true;
- else return false;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Enter your statement here: ");
- string statement = Console.ReadLine();
- bool check = isWellFormatted(statement);
- if(check == true)
- {
- Console.WriteLine("Your statement is well formatted.");
- }
- else
- {
- Console.WriteLine("Your statement is not formatted right.");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment