cpp lecture 6.pptx
- Количество слайдов: 30
CSS 203 Introduction to C++ Lecture 6 Instructor: Bakhit Bakiev
string A string variable s 1 could be declared as follows: char s 1[10]; The string variable s 1 could hold strings of length up to nine characters since space is needed for the final null character. Strings can be initialised at the time of declaration just as other variables are initialised. For example: char s 1[] = "example"; char s 2[20] = "another example" would store the two strings as follows: s 1 |e|x|a|m|p|l|e| | s 2 |a|n|o|t|h|e|r| |e|x|a|m|p|l|e| |? |? | In the first case the array would be allocated space for eight characters, that is space for the seven characters of the string and the null character. In the second case the string is set by the declaration to be twenty characters long but only sixteen of these characters are set, i. e. the fifteen characters of the string and the null character. Note that the length of a string does not include the terminating null character.
For example to read in a name in the form of a name followed by a surname we might use code as follows: char name[12], surname[12]; cout << "Enter name "; cin >> name; cin >> surname; cout << "The name entered was “ << name << " “<< surname;
C-strings vs C++ strings A C-string is a zero terminated array of chars but the C++ string does without the extra terminating zero. Most likely it keeps track of the length internally but you should not make any assumptions about how the implementation works. C-strings: #include
Assigning to a C-string variable: char str[10]; str = "Hello!"; Assigning to a C++ string object: string str; str = "Hello!"; Assigning other string: str = other. String; Concatenating two C-strings: strcat(str 1, str 2); strcpy(str, strcat(str 1, str 2)); Concatenating two C++ string objects: str 1 += str 2; str = str 1 + str 2;
Copying a C-string variable: char str[20]; strcpy(str, "Hello!"); strcpy(str, other. String); Copying a C++ string object: string str; str = "Hello"; str = other. String; Accessing a single character of C-string variable: str[index]; Accessing a single character of C++ string object: str[index]; str. at(index); str(index, count);
Comparing two C-strings: if (strcmp(str 1, str 2) < 0) cout << "str 1 comes 1 st. "; if (strcmp(str 1, str 2) == 0) cout << "Equal strings. "; if (strcmp(str 1, str 2) > 0) cout << "str 2 comes 1 st. "; Comparing two C++ string objects: if (str 1 < str 2) cout << "str 1 comes 1 st. "; if (str 1 == str 2) cout << "Equal strings. "; if (str 1 > str 2) cout << "str 2 comes 1 st. "; Finding the length of a C-string: strlen(str); Finding the length of a C++ string: str. length();
Output of a C-string variable: cout << str; cout << setw(width) << str; Output of a C++ string object: cout << str; cout << setw(width) << str; //cursor starts after width number of spaces Input of a C-style string variable : cin >> s; cin. get(s, num. Ch+1); cin. get(s, num. Ch+1, 'n'); cin. get(s, num. Ch+1, 'x'); cin. getline(s, num. Ch+1, 'n'); cin. getline(s, num. Ch+1, 'x'); Input of a C++ string object: cin >> s; getline(cin, s); //takes all like previos. getline(cin, s, 'x'); // takes till x
The setw(width) I/O manipulator can be used before outputting a string, the string will then be output right-justified in the field width. If the field width is less than the length of the string then the field width will be expanded to fit the string exactly. Notes: 1. Keep in mind that cin ignores white space when reading a string, while cin. get(), cin. getline() and getline() do not. 2. cin. getline() and getline() consume the delimiter while cin. get() does not.
str[0] = toupper (str[0]); The function toupper( ) or tolower( ) is a Standard Library facility related to character processing; this means that when using it, we have to include the
Explaining with examples 1. #include
2. string result; string s 1 = "hello "; string s 2 = "world"; result = s 1 + s 2; // result contains "helloworld" 3. string result; string s 1 = "hello"; // without the extra space at the end string s 2 = "world"; result = s 1; result += ' '; // append a space at the end result += s 2; // result now contains "hello world"
4. string firstname, lastname, fullname; cout << "First name: "; getline (cin, firstname); cout << "Last name: "; getline (cin, lastname); fullname = lastname + ", " + firstname; cout << "Fullname: " << fullname << endl;
<< >> operators : string str; cout << "Enter string for testing : "; cin >> str; cout << "n. String is : " << str << endl; cout << "Enter string for testing " << "(d to quit) : "; while ( cin >> str ) { cout << endl; cout << "String is : " << str << endl; cout << "Enter string for testing "<< "(d to quit) : "; OUTPUT: Enter string for testing : first String is : first Enter string for testing (d to quit) : second String is : second Enter string for testing (d to quit) : third String is : third Enter string for testing (d to quit) : }
+ += = operators : string str = "Hello"; cout << "str is : " << str << endl; str += ", "; str += ' '; cout << "str is : " << str << endl; string s; s = str + "World"; cout << "s is : " << s << endl; char ch = '!'; s += ch; cout << "s is : " << s << endl; OUTPUT: str is : Hello, s is : Hello, World!
Append: string str = "Nobody is perfect"; string s = ""; // empty string char ch[] = "abcdef"; s. append(str, 0, 6); cout << "s is : " << s << endl; // append string str at the end of s; string: : iterator inp. It 1 = str. begin()+6; string: : iterator inp. It 2 = str. end(); s. append(inp. It 1, inp. It 2); cout << "s is : " << s << endl; // appends copies of the characters in the range [inp. It 1, inp. It 2] to s s. append(3, '!'); cout << "s is : " << s << endl; // appends three ! s. append(ch, 3); cout << "s is : " << s << endl; OUTPUT: // s is : Nobody is perfect!!!abc // s is : Nobody is perfect!!!abcabc
Assign: string str = "Nobody is perfect"; string s = “ "; char ch[] = "Robert Frost"; s. assign(str); cout << "s is : " << s << endl; //assigns a copy of str to s; s. assign(str, 10, 7); cout << "s is : " << s << endl; //assigns to s a copy of the n characters in str, starting at position 10 s. assign(ch, 6); cout << "s is : " << s << endl; // assigns to s a string consisting of the first n characters in ch s. assign(ch); cout << "s is : " << s << endl; // assigns to s a copy of ch s. assign(str. begin(), str. end()); cout << "s is : " << s << endl; // assigns to s a string consisting of the characters in the range str. begin(), str. end(); s. assign(17, '*'); cout << "s is : " << s << endl; // assigns to s a string consisting of n copies of ch; OUTPUT: s is : Nobody is perfect s is : Robert Frost s is : Nobody is perfect s is : *********
Begin: string str = "C++ is best computer language"; string: : iterator It = str. begin(); while ( It != str. end() ) { if ( It[] == ' ' ) It[] = 'n'; cout << It[]++; } cout << endl; // Returns an iterator positioned at the first character in a string OUTPUT: C++ is best computer language
Reverse: int main () { string str = “SDU"; cout << str. reverse() << endl; return 0; } OUTPUT: UDS
Swap: string str 1 = "Robert"; string str 2 = "Forest"; cout << "str 1 is: " << str 1 << endl; cout << "str 2 is: " << str 2 << endl; cout << "swap str 1 with str 2" << endl; str 1. swap(str 2); cout << "str 1 is: " << str 1 << endl; cout << "str 2 is: " << str 2 << endl; OUTPUT: str 1 is: Robert str 2 is: Forest swap str 1 with str 2 str 1 is: Forest str 2 is: Robert
Copy: // string: : copy #include
Length: int main () { string str = "C++ is best computer language"; cout << "str is: " << str << endl; cout << "Length of str is : " << str. length() << endl; return 0; } OUTPUT: str is: C++ is best computer language Length of str is : 29
Size: #include
Resize: #include
max_size: // Returns the maximum number of characters that the string object can hold. // comparing size, length, capacity and max_size #include
Clear string: The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. // string: : clear #include
find #include
These are only few string’s ready functions. Some others like : ( insert , replace, rfind, rbegin , end, substr , c_str, capacity, compare, empty, erase, find_first_of , find_first_not_of , find_last_of, find_last_not_of ), you can find and read them from internet!