Having Some Fun With Strings

Let’s have some fun with some strings shall we? I decided to see what all I could do while using String.Format(), and StringBuilder(). Also taking advantage of a single dimensional array, char arrays, and string padding. The code may look like it’s jumping around. Like I said I was just doing some messing around. This wasn’t really a project. Just me trying to see what would work and what wouldn’t.

No screenshot for this one. You’ll see the Default.aspx code. It’s self explanatory. 🙂

Default.aspx Code File Contents

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ChallengePhunWithStrings.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">

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

        <br />


Default.aspx Code Behind Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChallengePhunWithStrings
    public partial class Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            StringBuilder resultLabelBuilder = new StringBuilder();

            // 1.  Reverse your name
            // In my case, the result would be:
            // Eric Cartman
            string name = "Eric Cartman";
            string reverseName = "";
            char[] arr = name.ToCharArray();        // Convert the string to a char array
            Array.Reverse(arr);                     // Reverse the char array.
            for (int i = 0; i < arr.Length; i++)
                reverseName += arr[i];              // Loop thru the array appending the reverseName string.

            // Build the string.
            resultLabelBuilder.Append("Puzzle 1 Results");
            resultLabelBuilder.Append("<br />");
            resultLabelBuilder.Append("<br />");
            resultLabelBuilder.Append("<br />");

            // 2.  Reverse this sequence:
            // When you're finished it should look like this:
            // Chewbacca,Han,Leia,Luke
            string names = "Luke,Leia,Han,Chewbacca";
            string[] splitNames = names.Split(',');     // Using the comma's in the string to add the names to an array.
            Array.Reverse(splitNames);                  // Reversing the string array.

            // Build the string in a comma delimited format.
            resultLabelBuilder.Append("Puzzle 2 Results");
            resultLabelBuilder.Append("<br />");
            resultLabelBuilder.Append(String.Join(",", splitNames));
            resultLabelBuilder.Append("<br />");
            resultLabelBuilder.Append("<br />");

            // 3. Use the sequence to create ascii art:
            // *****luke*****
            // *****leia*****
            // *****han******
            // **Chewbacca***
            resultLabelBuilder.Append("Puzzle 3 Results");
            resultLabelBuilder.Append("<br />");
            for (int i = 0; i < splitNames.Length; i++)
                resultLabelBuilder.Append(PadBoth(splitNames[i], 14));
                resultLabelBuilder.Append("<br />");
            resultLabelBuilder.Append("<br />");

            // 4. Solve this puzzle:

            string puzzle = "NOW IS ZHEremove-me ZIME FOR ALL GOOD MEN ZO COME ZO ZHE AID OF ZHEIR COUNZRY.";

            // Once you fix it with string helper methods, it should read:
            // Now is the time for all good men to come to the aid of their country.

            resultLabelBuilder.Append("Puzzle 4 Results");
            resultLabelBuilder.Append("<br />");
            char[] puzzleToCharArray = puzzle.ToLower().ToCharArray();  // Putting the string into a char array and making all the characters lowercase.
            char[] charArrayPuzzleTrim = new string(puzzleToCharArray).Remove(10, 9).ToCharArray();     // Removing the unwanted characters from the array.
            string newPuzzle = "";

            // Adding the characters from the char array into the string.
            for (int i = 0; i < charArrayPuzzleTrim.Length; i++)
                newPuzzle += charArrayPuzzleTrim[i];

            // Replacing all the z characters with t.
            string newPuzzleReplace = newPuzzle.Replace("z", "t");

            // Basically do all of the below tests with StringBuilder.
            resultLabelBuilder.Append(newPuzzleReplace.First().ToString().ToUpper() + newPuzzleReplace.Substring(1));

            // Add the label to finish the puzzles.
            resultLabel.Text = resultLabelBuilder.ToString();
            resultLabelBuilder.Append("<br />");

            // Doing some testing on getting the final string.
            //string test = newPuzzleReplace.First().ToString().ToUpper();    // Getting the first letter and making it caps.
            //string testOne = newPuzzleReplace.Substring(1);                 // Getting all the letters minus the first one.
            //string finalTest = test + testOne;                              // Putting the sentence back together.

            //testLabel.Text = finalTest;

        // Helper method used to pad the left and right side of strings.
        public string PadBoth(string source, int length)
            int spaces = length - source.Length;
            int padLeft = spaces / 2 + source.Length;
            return source.PadLeft(padLeft, '*').PadRight(length, '*');

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s