CS 111 - Week 10 Lecture 1 - 2016-10-25
WHAT IF ...
...we considered a C++ string class instance
to be a LIST of CHARACTERS?
* we could define our own function is_empty
to tell us if the list-of-char is empty;
(done!)
* hey, here is another method of the string class!
the at method expects a position in the string,
where 0 is the position of the 1st char in the string
(0 "steps" from the beginning)
and (length - 1) is the position of the last char
in the string...
const string COURSE = "CS 111";
COURSE.length() == 6
COURSE.at(0) == 'C'
COURSE.at(1) == 'S'
COURSE.at(2) == ' '
...
* there is another string method called
substr <- substring
One version of substr expects the position
to start at in the calling string, and it returns
the string of the chars from that position
to the end of the calling string
(REMEMBER the first char is at position 0!!!)
const string COURSE = "CS 111";
COURSE.substr(3) == "111"
COURSE.substr(4) == "11"
* hey, now we can write a TEMPLATE for a function
that "walks through" a C++ string as a "list of char"
/**
ret_type a_char_list_funct(string char_list, ...)
{
if (is_empty(char_list))
{
...;
}
else
{
... first(char_list) ...
a_char_list_funct( rest(char_list), ...) ...;
}
}
**/
...and try it out creating
silly_length,
and with the help of is_vowel, count_vowels