0% found this document useful (0 votes)
71 views

CS109A Notes For Lecture 3/15/96 Representing Strings

This document discusses different approaches for representing and storing strings in computer programs. It notes that the optimal representation depends on the operations needed on the strings and whether string lengths are fixed or variable. For single strings, a character array with an endmarker is best if lengths are fixed, while a linked list allows for variable lengths. When storing many strings, two approaches are discussed: using a pointer to the string location and endmarker, or storing a pointer and length. The pointer and length approach uses more space but enables quick access to string lengths.

Uploaded by

savio77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

CS109A Notes For Lecture 3/15/96 Representing Strings

This document discusses different approaches for representing and storing strings in computer programs. It notes that the optimal representation depends on the operations needed on the strings and whether string lengths are fixed or variable. For single strings, a character array with an endmarker is best if lengths are fixed, while a linked list allows for variable lengths. When storing many strings, two approaches are discussed: using a pointer to the string location and endmarker, or storing a pointer and length. The pointer and length approach uses more space but enables quick access to string lengths.

Uploaded by

savio77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS109A Notes for Lecture 3/15/96

Representing Strings

Key issue: what operations will be performed on


strings?
Do the strings themselves change?
e.g., text editor storing lines of text.
Does the set of strings change?
e.g., compiler maintaining the set of valid
identi ers as it examines a program.
What operations do we need to support?
Dictionary operations? Finding length?
Etc.
Do we have a limit on string length?
e.g., a personal record system that only
stores 30 characters for names, addresses.

Storing Single Strings

Several approaches:
1. Character array with endmarker \0 if there is
a length limit.
2. Linked list of characters if the strings change
length without bound.
But pack several characters per cell to
save space, since pointers are 4{8 characters long.

Mass Storage of Strings

Often we need to store many strings in a large area


(character array).
Example: Spelling checkers, searching text for
many keywords.
Approaches:
1

1. String = pointer to place in storage array


where word begins. Word terminated by
marker, e.g., \0.
2. String = record with pointer to beginning of
word in storage, plus length of word.
Method (1) saves space, since it requires an
extra character endmarker, vs. an integer
length.
However, method (2) might be better if we
frequently needed to check the length of
strings.
e.g., a Xword puzzle solver that searches
for words of given length with 0 or more
known letters.

You might also like