Download presentation
Presentation is loading. Please wait.
Published byPaula Lesley Griffith Modified over 8 years ago
1
CS 1430: Programming in C++ 1
2
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout << “str1: ” << str1 << endl << “str2: ” << str2; str2 = str1; 2
3
C++ Class string if (str1 == “CS2340”) cout << “Programming in VB!”; if (str1 > str2) cout << “str1 appears after str2 in a dictionary.”; else if (str1 < str2) cout << “str1 appears before str2 in a dictionary.”; else cout << “str1 is the same as str2.” str1 is less than str2 if word str1 is listed before str2 in a dictionary (ASCII code ordering) 3
4
C++ Class string cout << “str1 has “ << str1.length() << “ chars.”; cout << “str2 has “ << str2.size() << “ chars.”; // Just like cin.get() and cin.eof() // They are objects of some classes 4
5
C++ Class string String is implemented as an array of char cin >> str1; cout << “The first char of str1 ” << str1[0]; cout << “The last char of str1 ” //<< str1[?]; << str1[str1.length() - 1]; // Change the first char of str1 to ‘A’. str1[0] = ‘A’; 5
6
C++ Class string SubString Function cin >> str1; cout << “The first three chars of str1: ” << str1.substr(0, 3); // substring starting at index 0 // with length 3 cout << “The last three chars of str1: ” << str1.substr(str1.length() – 3, 3); 6
7
C++ Class string getline(cin, str1, ‘\n’); // not a member function of class string int pos = str1.find(“ ”); // a member function of class string str1[pos] = ‘_’; cout << str1; // Change the first space to ‘_’. 7
8
C String const int NAME_LENGTH = 15; char LastName[NAME_LENGTH + 1]; // One more for the null char ‘\0’. cout << "Enter last name: "; cin >> LastName; // No loops needed! // C++ reads chars until White Characters, // then inserts a ‘\0’. // The array is treated as a string. // and ended with a null char ‘\0’. 8
9
C String cout << "The last name is " << LastName; // C++ displays one char at a time // until a '\0' is reached. What if there is no null character? 9
10
C String char name1[16], name2[16]; // Up to 15 chars! cout << "Enter last name: "; cin >> name1; name2 = name1; // Can we do this? cin >> name2; if (name1 == name2) // Can we do this? cout << “Same name!”; NO! 10
11
C String Functions #include //#include Three functions: int strlen(const char str[]); void strcpy(char dest[], const char src[]); int strcmp(const char str1[], const char str2[]); 11
12
C String Functions #include char name1[16], name2[16]; cout << "Enter last name: "; cin >> name1; name2 = name1; // Valid? // NO! strcpy(name2, name1); // Yes! cout << “name1 has ” << strlen(name1) << “ chars.”; 12
13
Function strcmp() The function compares two strings one char at a time, and stops the first time the two strings have different chars or a null char is reached for both str1 and str2. The function returns the difference of the chars at the stopping postion of the two strings. Return value from strcmp(srt1, srt2) Result 0 str1 the same as str2 > 0 str1 is larger than str2 (later in a dictionary) < 0 str1 is smaller than str2 (earlier in a dictionary) str1 str2 strcmp(srt1, srt2) “CS143” “CS143” ? “CS1430” “CS143” ? “CS143” “CS1430” ? “CS113” “CS143” ? “100” “99” ? 13
14
C String Functions #include char name1[16], name2[16]; cin >> name1 >> name2; int result = strcmp(name1, name2); if (result == 0) cout << “Same string.”; else if (result < 0) cout << “name1 is smaller than name2.”; else cout << “name1 is larger than name2.”; 14
15
Function strcpy() //----------------------------------------------- // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: ( ?, ? ) // Parameter: (out, in) //----------------------------------------------- void strcpy(char dest[], const char src[]) { for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i]; return; } // Correct? 15
16
Function strcpy() //----------------------------------------------- // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: (out, in) //----------------------------------------------- void strcpy(char dest[], const char src[]) { for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i]; dest[i] = ‘\0’; // Copy the NULL character. return; } // Correct? 16
17
Function strcpy() //----------------------------------------------- // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: (out, in) //----------------------------------------------- void strcpy(char dest[], const char src[]) { int i; for (i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i]; dest[i] = ‘\0’; return; } 17
18
Function strcpy() //----------------------------------------------- // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: (out, in) //----------------------------------------------- void strcpy(char dest[], const char src[]) { int i = 0; while (src[i] != ‘\0’) { dest[i] = src[i]; i ++; } dest[i] = ‘\0’; return; } 18
19
Function strlen() //--------------------------------------------- // The function has one parameter: // str[], array of char. // The function finds and returns the length of // str[], excluding the null // char at the end. // Parameter: ( ? ) // Parameter: ( IN ) //--------------------------------------------- int strlen(const char str[]) { int size = 0; while (str[size] != ‘\0’) size ++; return size; } 19
20
Function strcmp() //------------------------------------------------------- // The function has two parameters: // str1[], array of char, null terminated, // str2[], array of char, null terminated. // The function returns an integer: // 0 when str1 is the same as str2 // positive when str1 > str2 // negative when str1 < str2. // Parameter: ( ?, ? ) // Parameter: ( IN, IN ) //------------------------------------------------------ int strcmp(const char str1[], const char str2[]) { int i; for (i = 0; str1[i] != ‘\0’ && str2[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]); } 20
21
Function strcmp() //------------------------------------------------------- // The function has two parameters: // str1[], array of char, null terminated, // str2[], array of char, null terminated. // The function returns an integer: // 0 when str1 is the same as str2 // positive when str1 > str2 // negative when str1 < str2. // Parameter: ( IN, IN ) //------------------------------------------------------ int strcmp(const char str1[], const char str2[]) { int i; // Can we check just str1[i]? for (i = 0; str1[i] != ‘\0’ && str2[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]); } 21
22
Function strcmp() //------------------------------------------------------- // The function has two parameters: // str1[], array of char, null terminated, // str2[], array of char, null terminated. // The function returns an integer: // 0 when str1 is the same as str2 // positive when str1 > str2 // negative when str1 < str2. // Parameter: (in, in) //------------------------------------------------------ int strcmp(const char str1[], const char str2[]) { int i; // Just check str1[i]? for (i = 0; str1[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]); } Very Good! 22
23
Schedule Prog5 Due Time: 10 PM, Thursday Lab10 Due 5 PM, Thursday Friday? 23
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.