ProgramGuru

String Programs

String Programs — purpose and prerequisites

String programs teach Unicode-aware iteration, searching, cleaning input, and formatting. JavaScript string length counts UTF-16 code units rather than user-perceived characters; for simple ASCII exercises this distinction may not matter, but real multilingual text needs attention.

What you’ll learn

  • Explain the purpose of string programs.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

Trimming removes surrounding whitespace; the regular expression splits on one or more whitespace characters. Spreading a string iterates code points, which is more appropriate than indexing UTF-16 code units for many cases.

Reversing Unicode code points is still not guaranteed to reverse grapheme clusters correctly; combined accents and emoji sequences need segmentation.

Visualize the flow

String Programs — learning flowText input → Split or iterate → String resultString Programs1Text input2Split or iterate3String resultRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

function wordCount(text: string): number {
  const trimmed = text.trim();
  return trimmed ? trimmed.split(/\s+/u).length : 0;
}
function reverseCodePoints(text: string): string { return [...text].reverse().join(""); }
console.log(wordCount(" TypeScript   makes sense "));
console.log(reverseCodePoints("code"));

Verified output

3
edoc

Compile: npx tsc --strict --target es2022 --module commonjs string-programs.ts; run: node string-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

Trimming removes surrounding whitespace; the regular expression splits on one or more whitespace characters. Spreading a string iterates code points, which is more appropriate than indexing UTF-16 code units for many cases.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Reversing Unicode code points is still not guaranteed to reverse grapheme clusters correctly; combined accents and emoji sequences need segmentation.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Count vowels case-insensitively and then try sentences with punctuation and multiple spaces.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes string programs in the example?
Choose one answer. You can try again.

Answer and explanation

Trimming removes surrounding whitespace; the regular expression splits on one or more whitespace characters.

Trimming removes surrounding whitespace; the regular expression splits on one or more whitespace characters. Spreading a string iterates code points, which is more appropriate than indexing UTF-16 code units for many cases.

Key idea to remember

Trimming removes surrounding whitespace; the regular expression splits on one or more whitespace characters. Spreading a string iterates code points, which is more appropriate than indexing UTF-16 code units for many cases.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Array Programs →