CPSC170
Fundamentals of Computer Science II

Lab 16: string class

strings

We have learned how to manipulate individual characters and character arrays. In practice, it is much more convenient to use the string class. It almost always better to use a professionally developed class, rather than reinventing the wheel by making a new class. You can use this class by including the header <string> and using the namespace std.

The following code illustrates a few functions in the string class. The class behaves like an array of characters. You can access the individual characters of an string as you would an array of characters. You can concatenate two string using the operator +. You can also input and output strings using the stream operators >> and << . The string class behaves like a fundamental C++ type.


#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main() {
   string str = "Cloud Strife";
   
   cout << "str = " << str << '\n';
   cout << "str.length() = " << str.length() << '\n';
   cout << "str[0] = " << str[0] << '\n';
   cout << "str[str.length()-1] = " << str[str.length() - 1] << '\n';
   cout << "str + str = " << str + str << '\n';

   str[5] = 'X';

   cout << "str = " << str << endl;
}

The string class provides many more functions. You can find a comprehensive list here. You need to memorize the names and functionality of ever public member function. Just kidding! You only need understand the functions you need to use. It always a good idea to go through the list of functions before implementing a new string function. You never know, what you need might already be implemented. Most functions have intuitive names, so you can easily guess what each function does. Each member function has its page that includes code to illustrate the use of the function. This is how you learn to use new classes. Remember, you don't need to understand every single detail about a class or its functions before you use it. In fact, at this stage you are likely to not understand a significant amount of the details of a class.

Exercises

Implement a function bool is_alphabetic(string). This function should take a string and return true if and only if the string is completely made of characters in the English alphabet. How should the parameter to this function be passes, by value, by reference, by constant reference? Don't forget your pre/post comments.

Implement a function void to_uppercase(string). This function should take a string and replace any lower case letter by its uppercase equivalent. It shouldn't do anything to any character of the sting that isn't lower case.

Look up the erase function of the string class (I'm intentionally not putting a link, I want you to get used to looking up information for yourself. If you can't find the required information, please talk to me or a TA). There are there versions of this function, focus on the one that takes in two integers. Use this function to implement a function called void clean(string) . This function should remove all characters other than characters in the alphabet. For example the string "isn't" should be transformed to "isnt", "2A" to "A", "123" to "". In general, after a call to this function, string should only contain only characters in the alphabet or be the empty string.

Implement a function bool is_palindrome(string) . This function should return true if and only if the string passed to it is a palindrome. A palindrome is a string that is equal to the reverse of itself. For example, "Ada", "Never odd or even", "Do geese see God?" (more interesting examples can be found on wikipedia). Make sure your function works on the examples given. The clean function should be of help.

A common step in natural language processing (NLP) applications is to strip a text file of its punctuation and transform all letters to lowercase (or uppercase) . Write a program that reads in a text file and outputs the content of the file without any punctuation. You program should also make all words lowercase. For example, given the following input file:

Alice was beginning to get very tired of sitting by her sister on the bank,
and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in it,
"and what is the use of a book," thought Alice, "without pictures or conversations?"

the output should be:

alice was beginning to get very tired of sitting by her sister on the bank
and of having nothing to do once or twice she had peeped into the
book her sister was reading but it had no pictures or conversations in it
and what is the use of a book thought alice without pictures or conversations

Similar to how we make a program read from an input file using < we can also write to a file using >. For example,

  a.out < input.txt > output.txt

This makes the program read from input.txt and write to output.txt. You might find the nonmember function getline useful for this problem. This is actually a problem where its way more efficient to use a character array rather than the string class. For now we won't concern ourselves with efficiency.

Converting to and from string

It is common to need to convert numbers to strings and strings to numbers. Functions that help with this can be found here. Read up on these functions and use them to complete the following exercise.

Exercises

Write a program that reads in a file of numbers and words. The numbers and words can appear in any order. Your program should print out the sum of all the numbers in the file. If there are no numbers it should return zero. On the following input file your program should return 64.

sony pony 16 Jill valentine
4.5 PC master race snow permanent 2 5.5
Lying choices Xbot 36 Nintendo direct drought