Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Modify the C# code below so that it can take in a sequence of strings that are larger than 2 and smaller than 50 in

Modify the C# code below so that it can take in a sequence of strings that are larger than 2 and smaller than 50 in length. The user will input how many strings, then enter each string. The output should show each string with either "balanced" or "unbalanced". Currently it only does this for one string.

using System; using System.Collections;

class MainClass { public static string checkParenthesis(string expr){ Stack st = new Stack(); bool isbalanced = true; for(int i=0;i

// if open bracket is encountered push it into the stack if(expr[i].Equals('[') || expr[i].Equals('(')){ st.Push(expr[i]); }

// if closing bracket is encountered then // check if stack is empty or not // if not empty then top of stack conteins the correspnding open bracket // if not then not balanced if(expr[i].Equals(']')|| expr[i].Equals(')')){ // stack is empty means closing bracket before opening bracket if(st.Count == 0){ isbalanced = false; break; }

// if the corresponding opening bracket is at top of stack then pop it from stack else if((expr[i].Equals(']') && st.Peek().Equals('[')) || (expr[i].Equals(')') && st.Peek().Equals('('))){ st.Pop(); }

// else wrong closing bracket so not balanced else{ isbalanced = false; break; } } } if(isbalanced && st.Count == 0) return " is balanced"; else return " is not balanced"; }

public static void Main (string[] args) { string expr = Console.ReadLine(); Console.Write(expr); Console.WriteLine(checkParenthesis(expr)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions