Hi
can someone help me to solve this problem.
there are some braces {{}{{}}}
How can I make a program in C# that can tell which opening brace is for which closing brace?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Roy S194 reputationPosted Aug 21, 2011, 10:36 AM EDT
using System.Collections.Generic;
namespace BraceTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(doBracesWork(Console.ReadLine()));
Console.ReadLine();
}
static string doBracesWork(string input)
{
Stack
string output = "";
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '{')
{
openingBraces.Push(i);
}
else if (input[i] == '}')
{
output += string.Format("{0} - {1}\n", openingBraces.Pop(), i);
}
}
return output;
}
}
}
Vilas GiteRank #951 · 2.1K reputationPosted Aug 21, 2011, 7:00 AM EDT
Hi Sunakshi!
In Visual Studio, at coding part when you typed braces then its automatically highlighted for itself.
Roy S194 reputationPosted Aug 21, 2011, 7:23 AM EDT
input:
"{{}{{}}}"
output:
1 - 2
4 - 5
3 - 6
0 - 7
I would store the indices of the openingbraces in a stack and when a closing brace is found then you know that the last opening brace that was found is paired with the current closing brace.
string doBracesWork(string input)
{
Stack
string output = "";
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '{')
{
openingBraces.Push(i);
}
else if (input[i] == '}')
{
output += string.Format("{0} - {1}\n", openingBraces.Pop(), i);
}
}
return output;
}
sunakshi saxena47 reputationPosted Aug 21, 2011, 10:29 AM EDT
sunakshi saxena47 reputationPosted Aug 21, 2011, 10:29 AM EDT
sunakshi saxena47 reputationPosted Aug 21, 2011, 11:57 AM EDT
That is suppose if i have a array like 1,2,3,4,5,6,7,8,9,10,11 than how can i get the sum of biggest sequential array from this array like if biggest sequential array from this is 8,9,10,11 hai so how can i get the sum.
VulpesRank #55 · 29.2K reputationPosted Aug 21, 2011, 3:41 PM EDT
In response to your other question, try the following:
Deepak Kumar167 reputationPosted Aug 21, 2011, 8:35 PM EDT
but i want some more help from your side.
first i don't want to do array sort, i want the sum of sub maximum sequential array. not maximum sum of sequential array.
like if array {1,2,3,4,10,11,5,21,22}
then i want the sum of 1,2,3,4 not {21,22} maximum sum of sequential array . I want the sum of largest sub array in length .
Please provide the answer as soon as possible .
Thanx in advance .
VulpesRank #55 · 29.2K reputationPosted Aug 22, 2011, 1:30 AM EDT
If there's more than one sequence with the same length, then the program returns the first sequence found.
It's only looking for ascending sequences. If you want to check for descending sequences as well then insert another for loop after the first one:
VulpesRank #55 · 29.2K reputationPosted Aug 22, 2011, 1:39 AM EDT