A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a C# program to reverse the strings contained in each pair of matching parentheses in a given string and also remove the parentheses within the given string
Q:

Write a C# program to reverse the strings contained in each pair of matching parentheses in a given string and also remove the parentheses within the given string

0

Write a C# program to reverse the strings contained in each pair of matching parentheses in a given string and also remove the parentheses within the given string

Sample Output:

pqrst
tsrqp
abhgefdcij

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Linq;
using System.Collections;

public class Example
    {
      public static string reverse_remove_parentheses(string str)
        {
            int lid = str.LastIndexOf('(');
            if (lid == -1)
            {
                return str;
            }
            else
            {
                int rid = str.IndexOf(')', lid);

                return reverse_remove_parentheses(
                      str.Substring(0, lid)
                    + new string(str.Substring(lid + 1, rid - lid - 1).Reverse().ToArray())
                    + str.Substring(rid + 1)
                );
            }
        }
  
   public static void  Main()
         {
            Console.WriteLine(reverse_remove_parentheses("p(rq)st"));
            Console.WriteLine(reverse_remove_parentheses("(p(rq)st)"));
            Console.WriteLine(reverse_remove_parentheses("ab(cd(ef)gh)ij"));
        }        
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now