/* stringdemo.cpp * Anthony Larrain * * Demonstrates working with the C++ string class */ #include #include using namespace std; int main(int argc, char *argv[]) { /* several ways to build strings */ string s1("tuesday"), s2(s1), s3 = "rainy", s4; /* can use the output operator on strings */ cout << "s1 is " << s1 << endl; cout << "s2 is " << s2 << endl; cout << "s3 is " << s3 << endl; cout << "s4 is " << s4 << endl; /* can find the length or size of a string */ cout << "The length of s1 is " << s1.length() << endl; cout << "The length of s2 is " << s2.size() << endl; cout << "The length of s3 is " << s3.length() << endl; cout << "The length of s4 is " << s4.size() << endl; /* can add characters to the end of a string */ s3.append(" night"); cout << "s3 is now " << s3 << endl; string linebreak("\n\n"); /* pad the string with 40 hyphens */ linebreak.append(40,'-'); /* add two newline characters to the end of the string */ linebreak.append("\n\n"); cout << linebreak; /* can check for empty string */ if(s4.empty()){ cout << "string s4 is empty" << linebreak; }else{ cout << "string s4 not empty" << linebreak; } /* can find characters in a string */ string line("hello;hola"); int pos = line.find(";"); cout << "The first occurrence of ';' is at position " << pos << linebreak; /* can return a substring of a string */ string english = line.substr(0,pos); string spanish = line.substr(pos + 1); cout << "The english word is " << english << endl; cout << "The spanish word is " << spanish << linebreak; /* more on find */ string record("fred;flint;555-1212;"); pos = record.find(";"); string first = record.substr(0,pos); record = record.substr(pos + 1); pos = record.find(";"); string last = record.substr(0,pos); string phone = record.substr(pos + 1); cout << "First name: " << first << endl << "Last name: " << last << endl << "Phone : " << phone << linebreak; /* can use the input operator on strings */ cout << "Enter your first name" << endl; cin >> s4; cout << "Hello " << s4 << endl; /* can read a line */ cout << "Enter your complete name " << endl; /* PROBLEM : Since we used the input operator above, the input operator * leaves the newline character in the stream.*/ cin.ignore(80,'\n'); // ships over newline character getline(cin,s4); cout << "Hello " << s4 << linebreak; /* operator < has been overloaded */ string last1("flinstone"), last2("rubble"); if(last1 < last2 ) { cout << last1 << " comes before " << last2 << linebreak; }else{ cout << last1 << " does not come before " << last2 << linebreak; } /* Note when a class overloads one relational operator typically * the remaining relational operators have been overloaded. */ string last3; last3 = last2; // overloaded assignment. /* operator == has been overloaded */ if(last3 == last2){ cout << "They are equal" << linebreak; } /* operator [] has been overloaded */ last3[0] = 'R'; cout << "Last2 is .. " << last2 << endl; cout << "Last3 is .. " << last3 << linebreak; /* operator != has been overloaded */ if(last2 != last3){ cout << "Now they are not equal" << linebreak; } /* operator + has been overloaded */ string name = "barney " + last2; cout << name << linebreak; /* operator += has been overloaded */ name += " is plays guitar"; cout << name << linebreak; system("PAUSE"); return EXIT_SUCCESS; }