Скачать презентацию CSS 203 Introduction to C Lecture 6 Instructor Скачать презентацию CSS 203 Introduction to C Lecture 6 Instructor

cpp lecture 6.pptx

  • Количество слайдов: 30

CSS 203 Introduction to C++ Lecture 6 Instructor: Bakhit Bakiev 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]; 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 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 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 C++ strings: #include Declaring a C-string variable: char str[10]; Declaring a C++ string object: string str; Initializing a C-string variables: char str 1[11] = "Call home!"; char str 2[] = "Send money!"; char str 3[] = {'O', 'K', ''}; char str 3[] = "OK"; Initializing a C++ string objects: string str 1("Call home!"); string str 2 = "Send money!"; string str 3("OK"); string str 4(10, 'x');

 Assigning to a C-string variable: char str[10]; str = 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, 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 << 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 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 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 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 library header: /* toupper example */ #include #include int main () { int i=0; char str[]="Test String. n"; char c; while (str[i]) { c=str[i]; putchar (toupper(c)); //tolower( c); i++; } return 0; } Output: TEST STRING

Explaining with examples 1. #include <string> using namespace std; string name; cout << Explaining with examples 1. #include using namespace std; string name; cout << "Enter your name: " << flush; cin >> name; // Or, alternatively: getline (cin, name); // read a whole line into the string name if (name == "") { cout << "You entered an empty string, “ << "assigning defaultn"; name = "John"; } else { cout << "Thank you, " << name << "for running this simple program!" << endl; }

2. string result; string s 1 = 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 << 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 << << >> 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 = + += = 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 = 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 = 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 : *********

 At : string s =

 Begin: string str = 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 Reverse: int main () { string str = “SDU"; cout << str. reverse() << endl; return 0; } OUTPUT: UDS

 Swap: string str 1 = 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 <iostream> #include <string> using namespace std; int Copy: // string: : copy #include #include using namespace std; int main () { size_t length; char buffer[20]; string str ("Test string. . . "); length=str. copy(buffer, 6, 5); buffer[length]=''; cout << "buffer contains: " << buffer << "n"; system("pause"); return 0; } OUTPUT: Buffer contains: string

 Length: int main () { string str = 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<iostream> #include<string> using namespace std; main() { string str( Size: #include #include using namespace std; main() { string str("We go to target"); cout<

 Resize: #include <iostream> #include <string> using namespace std; int main () { size_t Resize: #include #include using namespace std; int main () { size_t sz; // size_t is an unsigned integral type. string str ("I like to code in C"); cout << str << endl; sz=str. size(); str. resize (sz+2, '+'); cout << str << endl; str. resize (14); cout << str << endl; system("pause"); return 0; } OUTPUT: I like to code in C++ I like to code

 max_size: // Returns the maximum number of characters that the string object can max_size: // Returns the maximum number of characters that the string object can hold. // comparing size, length, capacity and max_size #include #include using namespace std; int main () { string str ("Test string"); cout << "size: " << str. size() << "n"; cout << "length: " << str. length() << "n"; cout << "capacity: " << str. capacity() << "n"; cout << "max_size: " << str. max_size() << "n"; system("pause"); return 0; } OUTPUT: size: 11 length: 11 capacity: 11 max_size: 4294967291

Clear string: The string content is set to an empty string, erasing any previous 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 #include using namespace std; int main () { string str; char c; cout << "Please type some lines of text. Enter a period to finish: n"; do { c=cin. get(); str += c; if (c=='n') { cout << str; str. clear(); } } while (c!='. '); system("pause"); return 0; }

find #include <iostream> #include <string> using namespace std; int main () { string str find #include #include using namespace std; int main () { string str ("There are some needles. "); string str 2 ("needle"); size_t found; found=str. find(str 2); if (found!=string: : npos) cout << "'needle' found at: " << found<< endl; system("pause"); return 0; } Output: 'needle' found at: 15

 These are only few string’s ready functions. Some others like : ( insert 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!