Discussion 4-18

Homework Tip:

Casting from a decimal to an integer can be scary. Round-off error can cause some pretty silly things to happen.

#include <iostream>
using namespace std;

// Example problem: I have x dollars. How many nickels is that?
// Assume that x can be converted into nickels exactly (i.e. x is a perfect multiple of 0.05)
int main() {

    // x = amount in dollars. Try different values for x.
    double x = 2.15;

    // Calculate amount of nickels
    int num_nickels = x / 0.05;

    // Output result to the screen.
    cout << "We started with x dollars. This is the same as "
         << num_nickels << " nickels.\n";
    return 0;

// One possible solution: store x as an integer (have x = 215 above).
// Now all division problems will give exact answers.

String-class functions

Each string comes with a bunch of functions that help you manipulate your data. We've already talked about using the 'string[]' function to access individual characters of your string. But there are a lot more things you can do. Below is a short list of the ones I find the most useful. For a complete list, check out the c++ reference page for string.

  1. The + operator for strings
    • You can concatenate strings using the + operator
    • Example:
              string str1 = "I want ";
              string str2 = "some pie!!";
              string final_string = str1 + str2;
              cout << final_string;
  2. string.size()
    • This function returns the number of characters in the string
    • For example:
              string myString = "Hello!";
              cout << myString.size();  // Prints 6
              myString = myString + "!!!!!!";
              cout << myString.size();  // Prints 12
    • string.size() will always be a non-negative whole number. We usually prefer the type size_t to store sizes. It will automatically choose between int, long int, etc.
  3. string.substr(unsigned begin, unsigned length)
    • This function returns a substring of your string. The substring will start at the position marked by begin, and will contain a number of characters equal to length.
    • Example:
              string myString = "I like turtles";
              string yourString = myString.substr(2,4);
              cout << yourString; // Prints 'like'
  4. string.find(string str)
    • This function searches your string for a substring equal to str. If it finds one then the function will return the starting position. If it doesn't find one then the function will return a value called string::npos
    • Example:
              string myString = "I like turtles";
              cout << myString.find("ike") << "\n"; // Prints 3
              cout << myString.find("rr") << "\n";  // What prints depends on your machine, 
                                                    // but it is equal to string::npos
    • Example #2:
              // What will this code segment do?
              string myString = "Xtreme Kool Letterz";
              int pos = myString.find("z");
              myString[pos] = 's';
              pos = myString.find("K");
              myString[pos] = 'C';
              pos = myString.find("X");
              myString[pos] = 'x';
              myString = "E" + myString;
  5. string.erase(unsigned pos, unsigned length)
    • This function erases a number of characters of your string. It starts at pos, and erases a total of length characters.
    • Example:
              string myString = "I like turtles";
              cout << myString << "\n";  // Prints "I turtles"

Note: A lot of these functions will do different things based on how many input arguments you give. For example, we can write

   string myString = "turtles";
   cout << myString.find("t", 2) << "\n";  // Prints 3
                           // The 2 tells the function to begin searching at position 2

   myString.erase(); // Erases the entire string
                     // In other words, it assumes you entered pos=0 and length=biggest possible number

Try experimenting around to find with different numbers of inputs to see what happens.

Practice Problems

Problem 1

Suppose I start out with a string called str. Write some code that swaps the second character from the left with the second character from the right. Example:

  Start with: Yummy foodstuffs
  End with: Yfmmy foodstufus

You can assume the string has at least two characters (otherwise this problem makes no sense.)

Problem 2

Suppose I have a string called name that stores a name in the format


Write some code that will rearrange the string into


Your code should work for all possible starting strings (i.e. anything that is two words, separated by a comma and a space).