Exercise: Strings

Build HTML and XML from strings, format and rearrange people's names, and try seven bonus word problems, with tests that check each method.

Apprentice · C# · Updated

A full-stack developer turns data into text all day: HTML for web pages, XML and other formats for other programs, names and labels for people to read. In this exercise you'll write 15 methods that do exactly that, and tests check each one.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-strings, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git.

The StringsExercise project has three classes of methods:

File Methods
Markup.cs ChangeCase, CreateHtml, MoreHtml, CreateXml, FormattedXml
NameFormatter.cs FormatFullName, CreateUserName, ReformatName
WordPlay.cs (bonus) Initials, FileExtension, MaskCardNumber, CountVowels, IsPalindrome, Censor, TitleCase

StringsExercise.Tests holds the tests. Open StringsExercise.slnx in Visual Studio, or the folder in VS Code.

What to do

Each method's comment says what it should return, with examples. Replace the return line with your own code, then run the tests with Test > Run All Tests, or dotnet test in Git Bash. Do Markup.cs and NameFormatter.cs first; WordPlay.cs is for when you want more.

A little HTML and XML

You'll meet HTML properly later in the course. For now you only need its shape: an element is a name in angle brackets, <p>, then its content, then the same name with a slash, </p>:

<p>This is a paragraph</p>
<strong>Bold text</strong>
<p />

XML has the same shape, but you choose the names to describe your data, such as <customer> and <name>. In XML, an element with no content can close itself, like the last line. HTML allows that only for empty elements such as <br> and <img>; a real web page writes <p></p>. This exercise uses the XML way.

Hints

  • Interpolation makes markup easy to read: quot;<{elementName}>{content}</{elementName}>".
  • New lines in a string are \n. In FormattedXml, the indent is two spaces.
  • Optional parts: check for "" before you add a middle name or a suffix, so no double spaces or stray commas appear.
  • Names: Split(' ') turns "Glen Carter Williamson" into three words. A word's first letter is word[0].
  • After a comma: IndexOf(',') finds it (or returns -1), and Substring cuts the name there.
  • Reuse: MoreHtml can call CreateHtml for elements that have content.

When you're done

Every test is green. Read your methods once more: is there a long chain of + that interpolation would make clearer? A repeated step a small method could hold?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-strings), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-strings.git
    git push -u origin main
    

    If you cloned the starter instead, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-strings.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-strings, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

Turning data into well-formed text, whether markup, usernames or display names, is one of the most common jobs in web development, and it's all built from a few string methods.

Key terms

Related