6.StringsFileIO

6.StringsFileIO - Strings y A string is composed of a...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Strings y A string is composed of a sequence of characters y In C++, a string can be represented using an array of char y A character array can store any sequence shorter than its total length y A special character called the null character , written as ‘ \0 ’, is used to signal the end of the sequence y Hence a character array of size n can only store a string with n ‐ 1 characters y Examples: char msg1[80] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; har msg2[80] = “Hello”; l t H e l l o \0 1 2 3 4 5 6 7 8 75 76 78 79 … … 1 char msg2[80] = Hello ; C++ allows a character array to be initialized using a string literal which is in fact a null ‐ terminated sequence of characters Simpler way to handle strings: String Class y As strings are frequently used in programs, C++ provides a class string (like data type) for easy string manipulations use the ass tring simply include the header file ring y To use the class string , simply include the header file string using the include directive , i.e., y A string object (like variable) can be declared using the class #include <string> name string , and initialized with any null ‐ terminated sequence as follows: char a = “Hello”; // initialized using an equal sign string msg1 = a; char a = “Hello”; // initialized using its constructors string msg1(a); 2 string msg2 = “World”; string msg3 = msg1; string msg2(“World”); string msg3(msg1); Assignment Statement y A string object may also be initialized or changed at a later time after its declaration using an assignment statement n assignment statement consists of a string object on the y An assignment statement consists of a string object on the left ‐ hand side of an equal sign , and a null ‐ terminated sequence or an expression that returns a string object on the right ‐ hand side y Examples: har a = “Hello”; char a = Hello ; string msg1, msg2, msg3; msg1 = a; sg2 = “World”; 3 msg2 = World ; msg3 = msg1; String Concatenation y Two strings can be concatenated to form a longer string using the binary operator + xamples: y Examples: string msg1 = “I love ”; string msg2 = “cats”; string msg3 = msg1 + msg2; “I love cats” “I love dogs” y Note that at least one of the operands must be a string object string msg4 = msg1 + “dogs”; string msg5 = “I hate ” + msg2; “I hate cats” in using the binary operator + y The following expression is therefore invalid 4 string msg = “I love ” + “lions”; 8 String Comparison y Strings can be compared lexicographically (dictionary order) using relational ( > , < , >= , <= ) and equality ( == , != ) operators xamples: y Examples: string msg1 = “Apple”, msg2 = “apple”; string msg3 = “apples”, msg4 = “orange”; bool c1 = msg1 == msg2; bool c2 = msg1 < msg2; bool c3 = msg2 < msg3; bool c4 = msg3 != msg4; false true y Again, at least one of the operands must be a string object bool c5 = msg4 > msg3; y Never compare two string literals directly 5 I/O with String Objects...
View Full Document

Page1 / 27

6.StringsFileIO - Strings y A string is composed of a...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online