Using strings in C0
The formal definition of Big O
Reviewing invariants (Homework 1)
A string is a sequence of characters. Unlike C, it is not accessed as an array of
characters (although it is stored this way internally). Instead, there are a
set of string operations in a library to help you work with strings:
Most of these functions are self-explanatory, but let's look at the last
function a little bit. When we compare strings, this is done using
lexicographical ordering. Each character is represented by an ASCII code
internally and these values are compared starting with the first character
of each string. If this pair has the same ASCII code, the compare function
moves on to the next second character in each string. This continues until
either one character has an ASCII value that is less than the other character's
ASCII value, one string runs out of characters to analyze (the shorter string
is "less" than the other), or both strings
run out of characters to analyze (strings must be equal then).
In the ASCII system, codes are given in order for uppercase letters (starting from
65 for 'A', 66 for 'B', etc.), lowercase letters (starting from 97 for 'a',
98 for 'b', etc.), and digits (starting from 48 for '0', 49 for '1', etc.).
So when letters are compared, this turns out to be alphabetical ordering
if only one case is used. Be careful when mixing cases. Based on lexicographical
ordering, here are a series of strings from "smallest" to "largest" lexicogrpahically:
The compare function returns a negative integer if the first string argument
is "less than" the second string argument (lexicographically), a positive
integer if the first string is "greater than" the second string (lexicographically)
or 0 if the two strings are equal.
Using strings
// Returns the length of the given string
int string_length(string s);
// Returns the character at the given index of the string.
// If the index is out of range, aborts.
char string_charat(string a, int idx);
// Returns a new string that is the result of concatenating b to a.
string string_join(string a, string b);
// Returns the substring composed of the characters of s beginning
// at index given by start and up to but not including the index
// given by end
// If end ≤ start, the empty string is returned
// If end < 0 or end > the length of the string, it is treated as
// though it were equal to the length of the string.
// If start < 0 the empty string is returned.
string string_sub(string a, int start, int end);
// Returns true if a and b are exactly the same, character for
// character, Returns false otherwise
bool string_equal(string a, string b);
// Returns a negative integer if a is lexicographically "less" than
// b, a positive integer if a is lexicographically "greater" than b,
// or 0 if a and b are equal.
int string_compare(string a, string b);
"Cat" < "Catch" < "ChEEse" < "CheX" < "Cheese" < "Chew"