The document provides an overview of C-strings in C++, detailing their definition, initialization, input methods, and various operations such as copying and concatenation. It also covers the use of C-strings as class members and introduces the C++ string class, highlighting its advantages and functionalities. Additionally, the document lists library functions for working with C-strings and explains string operators and member functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
8 views32 pages
9.the String Class
The document provides an overview of C-strings in C++, detailing their definition, initialization, input methods, and various operations such as copying and concatenation. It also covers the use of C-strings as class members and introduces the C++ string class, highlighting its advantages and functionalities. Additionally, the document lists library functions for working with C-strings and explains string operators and member functions.
adjacent memory locations and terminated by NULL character • String literal (string constant): sequence of characters enclosed in double quotes " " : "Hi there!" H i t h e r e ! \0
Array of C-strings const int elements=5; const int elementlen=10; char arrstrings[elements][elementlen] = { "abc","def","ghi","jkl","mno"}; //Array of strings as multidimentionsal array of chararcters for (int j = 0; j<elements; j++) cout << arrstrings[j] << endl; //display every string
Library Functions for Working with C-Strings Functions: – strlen(str): returns length of C-string str char city[SIZE] = "Missoula"; cout << strlen(city); // prints 8 – strcat(str1, str2): appends str2 to the end of str1 char location[SIZE] = "Missoula, "; char state[3] = "MT"; strcat(location, state); // location now has "Missoula, MT"
The C++ string Class • Special data type supports working with strings • #include <string> • Can define string variables in programs: string firstName, lastName; • Can receive values with assignment operator: firstName = "George"; lastName = "Washington"; • Can be displayed via cout • Use cin >> to read an item into a string
Input/output with string objects string full_name, nickname; cout << "Enter your full name : "; getline(cin, full_name); //reads embedded blanks cout << "Your full name is : "<< full_name << endl;
string Comparison • Can use relational operators directly to compare string objects: string str1 = "George", str2 = "Georgia"; if (str1 < str2) cout << str1 << " is less than " << str2; • Result is true or false
Other Definitions of C++ strings Definition Meaning string name; defines an empty string object string myname("Chris"); defines a string and initializes it string yourname(myname); defines a string and initializes it string aname(myname, 3); defines a string and initializes it with first 3 characters of myname
string verb(myname,3,2); defines a string and initializes it with 2
characters from myname starting at position 3
string noname('A', 5); defines string and initializes it to 5 'A's
string Operators OPERATOR MEANING >> extracts characters from stream up to whitespace, insert into string << inserts string into stream = assigns string on right to string object on left += appends string on right to end of contents on left + concatenates two strings [] references character in string using array notation >, >=, <, relational operators for string comparison. Return true or <=, ==, != false
cin >> word1; // word1 is "Hot" word2.assign(" Dog"); phrase.append(word1); phrase.append(word2); // phrase has "Hot Dog" phrase.append(" with mustard relish", 13); // phrase has "Hot Dog with mustard" phrase.insert(8, "on a bun "); cout << phrase << endl; // displays // "Hot Dog on a bun with mustard"