ExpandCollapsePrev Next Index

+ 5.1 Stripping leading and trailing spaces from a string

Unfortunately there's a bug in this program: readln reads in a line including the newline. We can fix this, and remove excess spaces at the same time as shown here:

  write$ stdout,"Enter your name: "; fflush stdout;
  var name = strip (readln$ stdin);
  writeln$ stdout, "Hello " + name + "!";

Bugs Bunny

Enter your name: Bugs Bunny
Hello Bugs Bunny!

This uses the function strip from the String module to remove leading and trailing whitespace, including newlines.