Click here to Skip to main content
13,145,774 members (65,451 online)
Rate this:
Please Sign up or sign in to vote.
See more:
static int Control(int x=0)
         string str = "(a(b(c)d)e)";
         for (int i = 0; i <= str.Length; i++)
             if (str[i] =='(')
             if (str[i] == ')')
         return x;
     static void Main(string[] args)
         int x = 0;
         bool flag = false;
         if (Control(x)==0)
             flag = true;
Posted 6-Jan-13 7:51am
Updated 6-Jan-13 7:54am
Mika Wendelius 6-Jan-13 13:54pm
What's the return value and what would you think it should be?

Also have you tried yet creating the recursive version?
Mika Wendelius 6-Jan-13 14:10pm
Also try using the debugger to see what actually happens. You can also try a bit different string. What happens with:

string str = "(a(b(cde)";

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You were ordered to write two functions, but you have written only one, incorrectly. Also, you did not explain your question properly.

From the very beginning: we are not going to do your homework for your. You are given a change to solve your assignments to learn something. Use this chance well.

Now, in your code there I can see two bugs. First, instead of '<=' you should use '<', in the ending condition of the loop. In the last element, you will get out-of-range exception; there are no elements at the index of [str.Length] or above.

The second one is the function signature. You don't need x as a parameter. Declare it locally.

One important practical advice: never ever create single-character names of any entities. How are you going to use "Find" functionality? How can you figure out what do they mean if you read your own code later? All the names should be semantic, maybe, not too long, but self-explaining and even without use of abbreviation: instead of "i" — "index"; instead of "x" — "count", or "balance".

Good luck with recursion version and everything else.

ridoy 6-Jan-13 15:14pm
Thank you,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100