Home
Intro
LC
ACR
About

Day 7 - Strings and Characters

-=-=-=-=-=-=-=-=-=-=-

What you are going to learn today:

  • What strings and characters are
  • How strings are implemented in Pascal
  • And the final example program!

-=-=-=-=-=-=-=-=-=-=-

Welcome back! This time, we're going to learn about strings and characters - storage space for text. Don't worry, this has nothing to do with real strings found on your sewing machine (put your needles away!), so just sit back, relax, and enjoy this tutorial.

Strings and Characters

Unless you're a caveman from the Stone Age and has never seen a line of text, you should be able to identify with strings and characters easily. You all know the integer and real types, so here's another two - enjoy!

'This is a string.'

Strings are text. Simple as that. I mean, just as how integers can store the value 15, strings can store things like "Life is like a box of chocolates", or "What, me worry?" Strings can contain any line of text 255 characters long or less. Any longer than that and the string will be truncated, so be forewarned! You can also use strings to store names, keywords, or just plain text. And as you'll soon find out, strings can be highly flexible things, so be careful!

'A', 'B', 'C'

On the other hand, characters don't store text. They can only store single characters, such as "a", "b", or "C". (Note that uppercase characters are different from lowercase ones.) This is highly useful, if you don't want to make one big string just to store one character. Imagine, you're going to make a box to store 255 teddy bears, but you're only going to put just one. Now, is that stupid or what?

How does Pascal implement them?

Ahh, C programmers would be happy to note that there are no ridiculous commands to memorise just to initialize a string. This is because the Pascal string design is much simpler. Instead of having some crappy null-terminated string, (don't worry, no technical details) Pascal has a string which contains the length of the string in the first byte. Yes, it may sound a little technical, but don't worry, you don't have to know that to use Pascal strings.

To start off, we'll first tell you how to use a string. In case you start thinking that you need to type mounds of code just to use a string, well no, all you have to do is to just declare a variable with the data type 'string', like this:

  var
    AString: string;
Uh-huh, that simple. And yes, how do you use them in code? Well, Pascal uses the apostrophe to mark out strings, just like how you typed text for the Writeln command. So, to assign AString the value of 'Sim Products Forever!', you type:
  begin
    AString := 'Sim Products Forever!';
  end.
Yes, now AString contains that wonderful line of text. Isn't that great? You can retrieve the value any time you want now - and yes, you can even add strings together to form one big string, like this:
  begin
    AString := 'Sim Products Forever!';
    AString := AString + ' Isn''t that great?';
    Writeln(AString);
  end.
    Rem - Two things to note: Firstly, notice the the line ' Isn't that great?' turned into ' Isn''t that great?' Why did the single apostrophe turn into a double one? Well, because the apostrophe is also the marker to tell Pascal that you want to end the string, to include an apostraphe itself, you would need to place double ones. Second, what if you want to create a string which can store less than 255 characters? (which is rarely the case unless you're really out of memory) Well, you just simply add a number representing the length when you declare the string - AString: String(50); That line will declare a string which can only store up to 50 characters, after which, everything extra will be truncated!

Now for characters. The case is mostly the same - just that you can only store one character only now. The data type for charcters is 'char'. Thus, (with an air of gusto), a typical character declaration looks like this:

  var
    AChar: char;
Yes! Magic is in the air! Now, we can give AChar a value the same way we do it with strings. Don't give a char a string value though, you'll get the infamous 'Error 26: Type mismatch.' error message from the compiler.
  begin
    AChar := '*';
  end.
Yep. That's it. The end of the story. We have a character holding the value '*', and voila! You have a perfect example of using a character. Not only that, but our simple introduction is over too! Well almost. Just below is the example program of the day. Happy Typing! (Hint of sarcasm)

The Example Program (Sacred Words)

As what the great poet Kasbury Hermes once said, "Strings are used to tie the characters of our lives together.", we are going to have an example program to do just that. (For you literature students flipping through your reference books there, you'll like to know that there's no such poet and no such quote. They were all products of my imagination, obviously.) This example program is so simple, it's going to bore you to death...

Type

1: program StringChar;
2:
3:   var
4:     Stringy: String;
5:     Charey: Char;
6:
7:   begin
8:     Stringy := 'Animaniacs';
9:     Writeln(Stringy);
10:    Charey := 'Z';
11:    Writeln(Charey);
12:  end.

Run

Animaniacs
Z

Analyse

What's there to say? What's there to explain? What's there... oh never mind. Here's the explanation for your personal reading pleasure:

  • Line 4 - This line declares the string Stringy
  • Line 5 - This line declares the character Charey
  • Line 8 - This line assigns Stringy the string "Animaniacs"
  • Line 9 - This line prints Stringy
  • Line 10 - This line assigns Charey the character 'Z'
  • Line 11 - And finally, this line prints out Charey!

Ending Words

Yes! You've finished Day 7! Now, that was simple, wasn't it? Well, get ready, 'cos it's gonna get complicated. Lift up your head, breathe hard, and go straight into Day 8 - User Defined Types.

-=-=-=-=-=-=-=-=-=-=-

HomeIntroLCACRAbout
Back to Class I

-=-=-=-=-=-=-=-=-=-=-

This page is ThinkQuest entry 11127.
email: tq97-11127@advanced.org