Skip to main content

Coding Challenge : Write an ASP.NET program to do string manipulation based on set of rules

 Given a string may contain spaces, convert them to

  • All first character caps
  • All Even numbers caps
  • All Odd Numbers small
  • Consider the strings are starting with 0 - Even number

Other conditions:

The given string is non null

 HTML

 

  <br />

        <br />

        <div>

            <p>

                Coding Challenge : Write an ASP.NET program to acheive the following!

            </p>

            <table style="width: 100%; border: dotted;" border="1">

                <tr>

                    <td>

                        <p>

                            Given a string may contain spaces, convert them to

                        </p>

                        <ul>

                            <li>All first character caps</li>

                            <li>All Even numbers caps</li>

                            <li>All Odd Numbers small</li>

                            <li>Consider the strings are starting with 0 - Even number</li>

                        </ul>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:TextBox ID="txtInput" Width="100%" runat="server"></asp:TextBox>

 

                    </td>

                </tr>

                <tr>

                    <td align="right">

                        <asp:Button ID="btnSubmit" runat="server" Text="Process" OnClick="btnSubmit_Click" />

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="lblResult" runat="server"></asp:Label>

                    </td>

                </tr>

            </table>

 

        </div>

 

C# Program

 

  protected void btnSubmit_Click(object sender, EventArgs e)

    {

        lblResult.Text = ProcessString(txtInput.Text);

    }

 

    public static string ProcessString(string s)

    { 

        List<string> LstResult = new List<string>();

 

        Func<char, bool, string> doProc = (chr, isTrue) =>

        {

            if (isTrue) return chr.ToString().ToUpper();

            else return chr.ToString().ToLower();

        };

 

        string[] StrArr = s.Split(' ').ToArray();

 

        foreach (var singleString in StrArr)

        {

            string returnVal = string.Empty;

            for (int i = 0; i < singleString.Length; i++)

            {             

                if (i == 0)

                {                  

                    returnVal+=doProc(singleString[i], true);

                }

                else if (i == 1)

                {

                    returnVal += doProc(singleString[i], false);

                }

                else if (i % 2 == 0)

                {

                    returnVal += doProc(singleString[i], true);

                }

                else

                {

                    returnVal += doProc(singleString[i], false);

                }

            }

            LstResult.Add(returnVal);

        }

 

        return string.Join(" ", LstResult);

    }


 

 


Comments

Popular posts from this blog

Task Parallel Library (TPL) and Akka.NET: Differences

Task Parallel Library (TPL) and Akka.NET are both powerful tools in the .NET ecosystem for handling parallelism and concurrency, but they serve different purposes and use different models of computation. Here are some key differences:s 1.    Actor Model vs Task-Based Model: Akka.NET is built around the actor model, where actors are the fundamental units of computation and they communicate by exchanging messages. TPL, on the other hand, is task-based. It's designed to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL uses tasks (which are independently executing work units) and provides various ways to control and coordinate them. 2.    Fault Tolerance: One of the key features of Akka.NET is its built-in fault tolerance. It has a "let-it-crash" philosophy, where the system is designed to self-heal from errors. If an actor fails, its parent actor can decide on the supervision strategy: either to resta

Extension Methods - Advanced

Here we will see how can we use the Extension Methods in advanced manner in other types Imagine you often need to retrieve items from a List based on a custom criterion that the built-in LINQ methods don't cover. Extension Methods for Lists: Filtering based on Custom Criteria And the output would be   Extending Enums: Displaying Descriptive Strings Output: Extending DateTime: Calculating Age     Output: The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods