1 / 21

Lecture 19:String Data Type

Lecture 19:String Data Type. Introduction to Computer Science Spring 2006. string Data Type. Programmer-defined type supplied in standard library A string is a sequence of zero or more characters Enclosed in double quotation marks Example: “ William Jacob ” “ Mickey ”

slonon
Download Presentation

Lecture 19:String Data Type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 19:String Data Type Introduction to Computer Science Spring 2006

  2. string Data Type • Programmer-defined type supplied in standard library • A string is a sequence of zero or more characters • Enclosed in double quotation marks • Example: “William Jacob” “Mickey” • Null (empty string): a string with no characters • Example: “” • Each character has relative position in string • Position of first character is 0, the position of the second is 1, and so on • For example, in the string “William Jacob”, the first character, 'W', in name is in position 0, the second character, 'i', is in position 1, and so on • Length: number of characters in string

  3. The string Type • To use the data type string, the program must include the header file <string> • The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob“ • The variable name is capable of storing any size string

  4. I/O operations on the string Type • An input stream variable (cin) and extraction operator >> can read a string into a variable of the data type string • Extraction operator • Skips any leading whitespace characters and reading stops at a whitespace character • Should not be used to read strings with blanks • The function getline • Reads until end of the current line • Should be used to read strings with blanks

  5. I/O operations on the string Type – Example Assume the input is: Break Back Mountain #include <iostream> #include <string> using namespace std; int main() { string mystring; cin>>mystring; cout<<mystring<<endl; return 0; } #include <iostream> #include <string> using namespace std; int main() { string mystring; getline(cin, mystring); cout<<mystring<<endl; return 0; } Output: Break Output: Break Back Mountain

  6. Relational operators on string Type: Comparing string Types • Relational operators can be applied to strings • Strings are compared character by character, starting with the first character • Comparison continues until either a mismatch is found or all characters are found equal • If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string • The shorter string is less than the larger string

  7. string Comparison Example • Suppose we have the following declarations: • string str1 = "Hello"; • string str2 = "Hi"; • string str3 = "Air"; • string str4 = "Bill";

  8. Other operators on the string Type • Binary operator + allows the string concatenation operation • For example, If str1 = "Sunny", the following statement stores the string "Sunny Day" into str2: str2 = str1 + " Day"; • The array subscript operator [] allows to access an individual character within a string • For example, If str1 = “Hello there", the following statement replaces the character t with the character T: str1[6] = ‘T’;

  9. Binary operator + and array subscript operator [] – Example #include <iostream> #include <string> using namespace std; int main() { string str1, str2, str3, str4; //Line 1 str1 = "Hello There"; //Line 2 cout << "Line 3: str1 = " << str1 << endl; //Line 3 str2 = str1;//Line 4 cout << "Line 5: str2 = " << str2 << endl; //Line 5 str1 = "Sunny"; //Line 6 str2 = str1 + " Day";//Line 7 cout << "Line 8: str2 = " << str2 << endl; //Line 8 str1 = "Hello"; //Line 9 str2 = "There"; //Line 10 str3 = str1 + " " + str2;//Line 11 cout << "Line 12: str3 = " << str3 << endl; //Line 12 str3 = str1 + ' ' + str2; //Line 13 cout << "Line 14: str3 = " << str3 << endl; //Line 14 str1 = str1 + " Mickey";//Line 15 cout << "Line 16: str1 = " << str1 << endl; //Line 16 str1 = "Hello there"; //Line 17 cout << "Line 18: str1[6] = " << str1[6] << endl; //Line 18 str1[6] = 'T';//Line 19 cout << "Line 20: str1 = " << str1 << endl; //Line 20 return 0; } Output: Line 3: str1 = Hello There Line 5: str2 = Hello There Line 8: str2 = Sunny Day Line 12: str3 = Hello There Line 14: str3 = Hello There Line 16: str1 = Hello Mickey Line 18: str1[6] = t Line 20: str1 = Hello There

  10. Some functions on string • length • size • find • substr • swap

  11. length Function • Length returns the number of characters currently in the string • The syntax to call the length function is: strVar.length() where strVar is variable of the type string • length has no arguments • length returns an unsigned integer • The value returned can be stored in an integer variable

  12. length function – Example #include <iostream> #include <string> using namespace std; int main() { string firstName; string name; firstName = "Elizabeth"; name = firstName + "Jones"; cout << firstName.length() << endl; cout << name.length() << endl; return 0; } Outputs 9 Outputs 15

  13. size Function • The function size is same as the function length • Both functions return the same value • The syntax to call the function size is: strVar.size() where strVar is variable of the type string • As in the case of the function length, the function size has no arguments

  14. size function – Example #include <iostream> #include <string> using namespace std; int main() { string firstName; string name; firstName = "Elizabeth"; name = firstName + "Jones"; cout << firstName.size() << endl; cout << name.size() << endl; return 0; } Outputs 9 Outputs 15

  15. find Function • find searches a string for the first occurrence of a particular substring • Returns an unsigned integer value of type string::size_type giving the result of the search • The syntax to call the function find is: strVar.find(strExp) or strVar.find(strExp, pos) • where strVar is a string variable and strExp is a string expression evaluating to a string • The string expression, strExp, can also be a character • If successful, find returns the position in strVar where the match begins • For the search to be successful the match must be exact • If unsuccessful, find returns the special value string::npos (“not a position within the string”)

  16. find function – Example //Example find function #include <iostream> #include <string> using namespace std; int main() { string sentence, str; //Line 1 string::size_type position; //Line 2 sentence = "Outside it is cloudy and warm."; //Line 3 str = "cloudy"; //Line 4 cout << static_cast<unsigned int> (sentence.find("is")) << endl; //Line 5 cout << static_cast<unsigned int> (sentence.find('s')) << endl; //Line 6 cout << static_cast<unsigned int> (sentence.find('o')) << endl; //Line 7 cout << static_cast<unsigned int> (sentence.find(str)) << endl; //Line 8 cout << static_cast<unsigned int> (sentence.find("the")) << endl; //Line 9 cout << static_cast<unsigned int> (sentence.find('i', 6)) << endl; //Line 10 position = sentence.find("warm"); //Line 11 cout << static_cast<unsigned int> (position) << endl;//Line 12 return 0; } Outputs 11 Outputs 3 Outputs 16 Outputs 14 Outputs 4294967295 Outputs 8 Outputs 25

  17. substr Function • substr returns a particular substring of a string • The syntax to call the function substr is: strVar.substr(expr1,expr2) where expr1 and expr2 are expressions evaluating to unsigned integers • The expression expr1 specifies a position within the string (starting position of the substring) • The expression expr2 specifies the length of the substring to be returned

  18. substr function – Example //Example substr function #include <iostream> #include <string> using namespace std; int main() { string sentence; //Line 1 string str; //Line 2 sentence = "It is cloudy and warm."; //Line 3 cout << sentence.substr(0, 5) << endl; //Line 4 cout << sentence.substr(6, 6) << endl; //Line 5 cout << sentence.substr(6, 16) << endl; //Line 6 cout << sentence.substr(17, 10) << endl; //Line 7 cout << sentence.substr(3, 6) << endl; //Line 8 str = sentence.substr(0, 8); //Line 9 cout << str << endl; //Line 10 str = sentence.substr(2, 10); //Line 11 cout << str << endl; //Line 12 return 0; } Outputs: It is Outputs: cloudy Outputs: cloudy and warm. Outputs: warm. Outputs: is clo Outputs: It is cl Outputs: is cloudy

  19. swap Function • swap interchanges the contents of two string variables • The syntax to use the function swap is strVar1.swap(strVar2); where strVar1 and strVar2 are string variables • Suppose you have the following statements: string str1 = "Warm"; string str2 = "Cold"; • After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm"

  20. swap function – Example //Example substr function #include <iostream> #include <string> using namespace std; int main() { string string1, string2; //Line 1 string1 = "Warm"; //Line 2 string2 = "Cold"; //Line 3 cout << "string1: " << string1 << " string2:" << string2 << endl;//Line 4 string1.swap(string2); //Line 5 cout << "string1: " << string1 << " string2:" << string2 << endl;//Line 6 return 0; }

  21. End of lecture 18 Thank you!

More Related