CS111 Lab
Strings
Instructor: Michael Gordon
The String class
 A class can be thought of as new type of
variable that you can create.
 C++ is also called “C with classes”
 We’ve used one kind of class already: the
string class, which is not in C.
 string and its functions are built into C++.
 C had C-strings, which can be found in
the cstring library.
Strings
 The internal construction of a string (or
cstring) is really an array of characters.
 The first character of string s is s[0].
 string s = “Hello”;
cout<<s[4]; //prints the letter o
 To find the length of a string s we can use
the functions .length() or .size()
 int x = s.length(); gives x the value of the
size of the string s (in this case, 5).
Strings
 To “concatenate” two strings s1 and s2:
 string s3 = s1 + s2;
 string s1 = “race”;
 string s2 = “car”;
 string s3 = s1 + s2;
 cout<<s3; //prints racecar
String examples
string first, last, name;
cout<<"Enter your first name:";
cin>>first;
cout<<"Enter your last name:";
cin>>last;
cout<<"Hi, "+first+" "+last;
//concatenation
getline()
 cin reads the typed string only until the first
whitespace.
 If you want the whole string including
those whitespaces, you can use:
 getline(cin, stringname);
 cout<<“Enter your name:”;
 getline(cin, fullname);
 Use fullname instead of first and last.
“dot” functions
 With strings we start dealing with a new
kind of function call. These functions
operate on a specific “object” (an
instance of a class).
 We write the object name (e.g. the
variable name of the string) followed by
period and the function call (no space
before or after the period).
More functions
 s1.find(string s2) – returns the index of the
beginning of s2 in s1.
 s1 = “hello”;
 s1.find(“lo”) – returns 3
 s1.insert(int i, string s2) – inserts s2 into s1 at
index I
 s1.at(int i) – returns the character at index
i in string s1

Strings1

  • 1.
  • 2.
    The String class A class can be thought of as new type of variable that you can create.  C++ is also called “C with classes”  We’ve used one kind of class already: the string class, which is not in C.  string and its functions are built into C++.  C had C-strings, which can be found in the cstring library.
  • 3.
    Strings  The internalconstruction of a string (or cstring) is really an array of characters.  The first character of string s is s[0].  string s = “Hello”; cout<<s[4]; //prints the letter o  To find the length of a string s we can use the functions .length() or .size()  int x = s.length(); gives x the value of the size of the string s (in this case, 5).
  • 4.
    Strings  To “concatenate”two strings s1 and s2:  string s3 = s1 + s2;  string s1 = “race”;  string s2 = “car”;  string s3 = s1 + s2;  cout<<s3; //prints racecar
  • 5.
    String examples string first,last, name; cout<<"Enter your first name:"; cin>>first; cout<<"Enter your last name:"; cin>>last; cout<<"Hi, "+first+" "+last; //concatenation
  • 6.
    getline()  cin readsthe typed string only until the first whitespace.  If you want the whole string including those whitespaces, you can use:  getline(cin, stringname);  cout<<“Enter your name:”;  getline(cin, fullname);  Use fullname instead of first and last.
  • 7.
    “dot” functions  Withstrings we start dealing with a new kind of function call. These functions operate on a specific “object” (an instance of a class).  We write the object name (e.g. the variable name of the string) followed by period and the function call (no space before or after the period).
  • 8.
    More functions  s1.find(strings2) – returns the index of the beginning of s2 in s1.  s1 = “hello”;  s1.find(“lo”) – returns 3  s1.insert(int i, string s2) – inserts s2 into s1 at index I  s1.at(int i) – returns the character at index i in string s1