Literals/String: Difference between revisions
Content deleted Content added
Line 13:
The length of a string in Ada is equal to the number of characters in the string. Ada does not employ a terminating null character like C. A string can have zero length, but zero length strings are not often used. Ada's string type is a fixed length string. It cannot be extended after it is created. If you need to extend the length of a string you need to use either a ''bounded string'', which has a pre-determined maximum length, similar to C strings, or an ''unbounded string'' which can expand or shrink to match the data it contains.
=={{header|ALGOL 68}}==
In ALGOL 68 a single character (CHAR), character arrays ([]CHAR) and strings (STRING) are contained in double quotes. ALGOL 68 also has FORMAT strings which are contained between dollar ($) symbols.
CHAR charx = "z";
Strings are contained in double quotes.
[]CHAR charxyz = "xyz";
STRING stringxyz = "xyz";
FORMAT twonewlines = $ll$, twonewpages=$pp$, twobackspaces=$bb$;
The STRING type is actually a union of CHAR and FLEX array of CHAR.
MODE STRING = UNION(CHAR, FLEX[]CHAR);
ALGOL 68 also has type BYTES, this is fixed width packed array of CHAR.
BYTES bytesabc = bytes pack("abc");
A string quote character is inserted in a string when two quotes are entered, eg:
STRING stringquote = """I'll be back."" - The Terminator";
A string can span lines, but cannot contain newlines. String literals are concatenated when compiled:
STRING linexyz := "line X;" +
"line Y;" +
"line Z;";
ALGOL 68 uses FORMATs for doing more advanced manipulations.
For example given:
FILE linef; STRING line;
associate(linef, line);
Instead of using preprocessor macros ALGOL 68 can use library FORMATs for doing variable replacement within FORMATs at run time.
FORMAT my_symbol = $"SYMBOL"$;
FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$;
putf(linef ,foo);
In <b>standard</b> ALGOL 68 a "book" is a file. A book composed of pages and lines.
A FORMAT be used for inserting backspaces, space, newlines and newpages into books.
INT pages=100, lines=25, characters=80;
FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]STRING book;
associate(bookf, book);
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5 "$)
Note: ALGOL 68G does not implement pages, lines and backspaces.
=={{header|C}}==
|