390 likes | 406 Views
Learn about string standards, declarations, assignments, I/O operations, comparisons, functions, and parameter passing in C++. Understand string literals, conversions, and differences between strings and characters or numbers.
E N D
Cannon_Chapter9 Strings and the string Class
Overview • Standards for Strings • String Declarations and Assignment • I/O with stringVariables • string Operationsand Comparisons • Functions for string • Parameter Passing with Strings
String Literal • A string literal is a constant string. • Example • A string is a sequence of characters such as a sentence or a single word. cout << "Hello World!"; infile.open("input.txt");
Standards for Strings #include<iostream.h> #include <string.h> Standard form • With Microsoft Visual C++ #include<iostream> #include <string> using namespace std; Microsoft form
String Declarations • Variables are declared with the data typestring • Example string variable, variable, ..; string word1, word2 = "hello", word3 = "goodbye";
String Assignment • string variables can be assigned from other string variables • Example word1 = "Hello World"; word2 = word1;
String vs.char • String constants or literals are represented with doublequotes such as "A". • String literal may contain only a single character. • Character constants are represented with single quotes such as 'A'.
Automatic Conversion string s; char c; s = "%"; c = '%'; s = '%'; // OK c = "%"; // ILLEGAL s = c; // OK c = s; // ILLEGAL
String vs.float • The difference between a number and a string literal containing the digit characters of that number • There is no automatic or default conversion: string text = "123.45"; float number = 123.45; number = text; // ILLEGAL text = number; // ILLEGAL
Null String • The null string is just a string containing no characters. • The null string is not the same as a string containing only blanks. • Example string str; // str contains the null string string word = ""; // word contains the null string word = " "; // word now contains a blank
Blank String • A blank is not “nothing”; it is a specific character. • Output: string word1 = "hello", space = " ", word2 = "world"; cout << word1 << space << word2;
<< operator with string variables • Output: • Output: string word1 = "hello", word2 = "world"; cout << word1 << word2; string word1 = "hello", word2 = " world"; cout << word1 << word2;
>> operator with string variables • The sequence of characters up to the next white spaceis placed within the variable. • Example: If user types "hello world", string word; cin >> word; //word contains "hello" string word1, word2; cin >> word1 >> word2; //word1 contains "hello" //word2 contains "world"
I/O with string variables(1) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;
I/O with string variables(2) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;
I/O with string variables(3) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;
I/O with string variables(4) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;
I/O with string variables(5) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;
I/O with string variables(6) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;
Example • Write a C++ program that • Input are first name, followed by a space, followed by lastname. • Output arelastname, followed by a comma and a space, followed by firstname.
Input and Output • Input: Output:
C++ Program #include <iostream.h> #include <fstream.h> #include <string> void main() { ifstream infile ("instructor.txt", ios::in); string firstname, lastname; while(infile >> firstname >> lastname) { cout << lastname << ", " << firstname << endl; } }
Function getline() • Read an entire line of text input to a string variable including white spaces or blanks #include <iostream.h> #include <fstream.h> #include <string> void main() { ifstream infile ("instructor.txt", ios::in); string name; while(getline(infile, name)) cout << name << endl; }
Input and Output • Input: Output:
string Operations • A string variable may be concatenated with another string using the + operator. • Concatenation means that the two strings are joined or pasted together. • Example string first = "Jim", last = " Thompson", name; name = first + last; cout << name << endl;//outputs "Jim Thompson"
string Operations (cont.) • A literal may be concatenated onto the end of a string variable. string first = "Jim", name; name = first + " Thompson"; cout << name << endl; //outputs "Jim Thompson" name = "Thompson, " + first; cout << name << endl;//outputs "Thompson, Jim"
string Operations (cont.) • Two literals cannot be concatenated into a string. • The shorthand operator += can be used: string name; name = "Jim" + " Thompson"; //ILLEGAL string name; name = "Jim"; name += " Thompson"; //outputs "Jim Thompson"
string Comparisons • string variables and literals can be compared using the relational operators (==, !=, <, >, <=, >=). • The first string is less than the second if its character in that position has an ASCII code less than the character in the same position in the other string. • Two literals cannot be compared. if ("Zeek" < "adam")... //ILLEGAL
Example #include <iostream.h> #include <string> void main() { string first, second; cout << "Enter two words: "; cin >> first >> second; if (first == second) cout << "Words are exactly equal."; else if (first < second) cout << "The first word is less than the second."; else cout << "The second word is less than the first."; }
Functions for string • The current length of a string variable can be determined using the length() function. • What is the output? string name = "Jim Thompson"; int size; size = name.length(); cout << "Length of “ << name << “ is " << size;
Functions for string(cont.) • The maximum length and a particular word of a string variable can be determined by using the max_size() and find() function respectively. string sentence; getline(cin, sentence); if (sentence.find("Jim", 0) < sentence.max_size()) cout << "The word \"Jim\" was found in this sentence!"; else cout << "The word \"Jim\" was found in this sentence!";
Functions for string(cont.) • The find() function doesn’t look for an independent word – just the sequence of characters J, i and m
Example #include <iostream.h> #include <fstream.h> #include <string> const string word = "nuclear"; const string phrase = "atomic energy"; void main() { string line; ifstream infile; infile.open("test.txt"); //Correct error in text book
Example (cont.) while (getline(infile,line)) { if (line.find(word, 0) < line.max_size()) cout << "WORD FOUND!" << endl; else if (line.find(phrase, 0) < line.max_size()) cout << "PHRASE FOUND!" << endl; } cout << "File completely examined." << endl; }
Parameter passing with strings • string variables may be used as parameters to functions. • Order() function will swap two strings if not in order and will return the shorter of the two.
Parameter passing with strings (cont.) string Order(string& first, string& second) { string temp; if (first >= second) { temp = first; first = second; second = temp; //Correct error in text book } if (first.length() < second.length()) return first; else return second; }
Conclusion • String is a representation of textual data. • String literal is a constant string. • Null string is a string containing no characters. • char variablescan hold or represent only a single character. • string variable can contain the digit characters of a number.
Conclusion (cont.) • The header file for string access does not have an .h extension. • string variables may be input and output using the >> and << operators. • string variable(s)and string literal(s) can be concatenated using the + operator. • Any of the six relational comparison operators may be used to compare two strings or a string with a literal.
Conclusion (cont.) • The length of a string variable can be determined using the length() function. • The maximum length of a string variable can be determined using the max_size() function. • Whether a string variable contains a particular substring or sequence can be tested with the find() function.