04-C++-Strings - CS106X Spring 2009 Handout 04 April 3 2009...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 04 Spring 2009 April 3, 2009 C++ Strings Original handout written by Neal Kanodia, with help from Steve Jacobson. C++ Strings One of the most useful data types supplied in the C++ libraries is the string. A string is a variable that stores a sequence of letters or other characters, such as " Hello " or " May 10th is my birthday !" . Just like the other data types, to create a string we first declare it, then we can store a value in it. string testString; testString = "This is a string."; We can combine these two statements into one line: string testString = "This is a string."; Often, we use strings as output, and cout works exactly like one would expect: cout << testString << endl; will print the same result as cout << "This is a string." << endl; In order to use the string data type, the C++ string header <string> must be included at the top of the program. Also, you’ll need to include genlib.h to make the short name string visible instead of requiring the cumbersome std::string . (As a side note, std is a C++ namespace for many pieces of functionality that are provided in standard C++ libraries. For the purposes of this class, you won't need to know about namespaces.) Thus, you would have the following #include 's in your program in order to use the string type. #include <string> #include "genlib.h" A string is basically a sequence of characters. Next, we will see what sorts of useful operations are supplied that allow you to manipulate strings. Basic Operations Let’s go into specifics about the string manipulations you’ll be doing the most. Counting the number of characters in a string. The length method returns the number of characters in a string, including spaces and punctuation. Like many of the string
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 operations, length is a member function , and we invoke member functions using dot notation . The string that is the receiver is to the left of the dot, the member function we are invoking is to the right, (e.g. str.length() ). In such an expression, we are requesting the length from the variable str . example program: #include <string> #include <iostream> #include "genlib.h" int main() { string small, large; small = "I am short"; large = "I, friend, am a long and elaborate string indeed"; cout << "The short string is " << small.length() << " characters." << endl; cout << The long string is " << large.length() << " characters." << endl; return 0; } output: The short string is 10 characters. The long string is 48 characters. Accessing individual characters. Using square brackets, you can access individual characters within a string, similar to as if the string were an array. Just as with array access, positions are numbered starting from 0, and the last position is (length - 1). Using square brackets, you can both read the character at a position and assign to that position. example program: #include <string> #include <iostream> #include "genlib.h" int main() { string test; test = "I am Q the omnipot3nt"; char ch = test[5]; // ch is 'Q' test[18] = 'e'; // we correct misspelling of omnipotent cout << test << endl; cout << "ch = " << ch << endl; return 0; } output: I am Q the omnipotent ch = Q
Image of page 2
3 Be careful not to access positions outside the bounds of the string! The square bracket
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern