



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The lecture on string and character operations in csci-1200 computer science ii. The lecture covers the use of maps, string manipulation, character operations, and an exercise to write a palindrome detection program. The document also includes an outline for solving text analysis problems.
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Koenig & Moo: Sections 5.6-5.
istream& getline(istream &, string &);
Returning the istream reference may seem a bit strange, but it is common practice. It allows the state of the stream to be tested in a conditional. We’ve seen this already with loops to read integers and strings, for example:
std::string name; while (std::cin >> name) { ... }
std::string s = "My name is Sally Jones"; std::string t = s.substr(11,5); // Starting at location 11, extract the next 5 chars. std::cout << t << std::endl; // Outputs: Sally
Each of these functions takes a character and returns true or false.
’c’ - ’a’ == 2 // this is true char(’B’ + 4) == ’F’ // this is true std::cout << ’a’ + 10 << std::endl; // outputs the integer 107 std::cout << char(’a’ + 10) << std::endl; // outputs the letter k
char c = ’P’ + 2; tolower(c); c
Now let’s address the text analysis posed at the beginning of the lecture. Here’s an outline of how you might approach solving problems like this, which do not involve the design of classes:
We want to analyzing an input text file to find:
Here’s one outline (others are certainly possible). Each of the (*) corresponds to a helper function.
unsigned int count_characters(const string& a_line) {
}
void add_to_letter_counts(const string & a_line, vector
}
vector
}
void count_word_occurrences(vector
}