Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings: C-strings vs. Strings as Objects

Similar presentations


Presentation on theme: "Strings: C-strings vs. Strings as Objects"— Presentation transcript:

1 Strings: C-strings vs. Strings as Objects
Andy Wang Object Oriented Programming in C++ COP 3330

2 C-style Strings C-string is a null-terminated array of type char
No built-in string type in C Must be char arrays NOT every char array is a C-string, only null- terminated ones

3 Library Features C++ <cstring> library provides functions to work with C-style strings strlen() // string length strcpy(), strncpy() // string copy strcat(), strncat() // string concatenation strcmp(), strncmp() // string comparison strstr() // string search strtok() // string tokenization

4 Some Implementation Details
int strlen(const char *s) { for (int j = 0; s[j] != ‘\0’; j++); return j; } char * strcpy(char *s1, const char *s2) { int j = 0; for (; s2[j] != ‘\0’; j++) s1[j] = s2[j]; s1[j] = s2[j]; return s1;

5 Some Implementation Details
int strcmp(const char *s1, const char *s2) { int j = 0; for (; ((s1[i] != ‘\0’) && (s1[i] == s2[i])); j++); return (s1[j] - s2[j]); } char *strcat(char *s1, const char *s2) { int i = strlen(s1), j = 0; for (; s2[j] != ‘\0; i++, j++) s1[i] = s2[j]; s1[i] = s2[j]; return s1;

6 C++ <iostream> Library
Provides I/O handling functions for C-style strings char str[40]; cout << str1; // insertion cin >> str1; // extraction cin.get(str1, 40, ‘,’); // reads to delimiter (comma) cin.getline(str1, 40); // reads to delimiter (default is endl) // discards delimiter

7 The DOWN Side of C-strings
Fixed length String name acts as a pointer Needs to be passed in and out of functions by reference Must be careful with array boundaries C-string boundaries are not automatically detected, even for library functions

8 More Hurdles for Common Ops
char greeting[40]; // can’t do greeting = “Hello World”; instead strcpy(greeting, “Hello World”); // can’t do if (str1 == str2); instead if (strcmp(str1, str2) == 0) A C coder needs to dynamically resize c-strings

9 C-String Pitfalls C-string library functions often assume the use of ‘\0’ to terminate a string What prints here? char vowel[5] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; cout << vowels; // is this a c-string? Here is an attempt to copy one C-string to another char greeting[25] = “Take me to your leader”; char welcome[10] = “Hello”; strcpy(welcome, greeting); // anything to worry about?

10 C-String Pitfalls Suppose we have enough space
char buffer[40] = “Dog” // length 3, capacity 39 char word2[] = “food”; // length 4, capacity 4 strcat(buffer, word2); // buffer = “Dogfood” strcat(buffer, “ breath”); // buffer // = “Dogfood breath” strcat(buffer, buffer); // should be enough room?

11 Example Non null terminating string
/pitfalls/output.cpp

12 output.cpp #include <iostream> using namespace std; int main() { char temperatures[3] = {'K', 'F', 'C'}; char vowels[5] = {'A', 'E', 'I', 'O', 'U'}; char directions[4] = {'N', 'S', 'E', 'W'}; char digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

13 output.cpp cout << "None of these are 'c-strings'. What if we print with << ?\n"; cout << "temperatures = " << temperatures << '\n'; cout << "digits = " << digits << '\n'; cout << "vowels = " << vowels << '\n'; cout << "directions = " << directions << '\n'; }

14 Example Self overwrite
/pitfalls/copy.cpp

15 copy.cpp #include <iostream> #include <cstring> using namespace std; int main() { char greeting[25] = "Take me to your leader"; char welcome[10] = "Hello"; cout << "greeting[] = " << greeting << '\n'; cout << "welcome[] = " << welcome << '\n';

16 copy.cpp strcpy(welcome, greeting);// anything to worry about? cout << "greeting[] = " << greeting << '\n'; cout << "welcome[] = " << welcome << '\n'; // WILL this behave the same on all systems? }

17 Example Self concatenation
/pitfalls/concat.cpp

18 concat.cpp #include <iostream> #include <cstring> using namespace std; int main() { char buffer[40] = "Dog"; // length 3, capacity 39 char word2[] = "food"; // length 4, capacity 4 cout << "buffer = " << buffer << '\n'; cout << "word2 = " << word2 << '\n';

19 concat.cpp strcat(buffer, word2); // buffer is now "Dogfood" cout << "buffer = " << buffer << '\n'; strcat(buffer, " breath"); // buffer is now // "Dogfood breath" strcat(buffer, buffer); // plenty of room? return 0; }

20 A String Wish List How should ideal strings behave?
Flexibility in storage capacity Simple assignment, comparison, concatenation Options to pass string objects by value, reference or const reference

21 Building a String class
Use dynamic allocation and resizing to get flexible capacity Do dynamic memory management inside the class Use operator overloads Insertion, extraction operators for easy IOs Comparison operator to ease sorting Operator+ for concatenation

22 Building a String class
Build copy constructor and assignment operator for assignment and pass-by-value capabilities Build conversion construction to convert c-style strings to string objects Could include conversion constructors for converting other types to strings, too

23 Example: BString class
strings/bstring/

24 bstring.h #include <iostream> using namespace std; class BString { friend ostream &operator<<(ostream &os, const BString &s); friend istream &operator>>(istream &is, BString &s); friend istream &getline(istream &is, BString &s, char delim='\n');

25 bstring.h friend bool operator<(const BString &s1, const BString &s2); friend bool operator>(const BString &s1, friend bool operator<=(const BString &s1, friend bool operator>=(const BString &s1,

26 bstring.h friend bool operator==(const BString &s1, const BString &s2); friend bool operator!=(const BString &s1, friend BString operator+(const BString &s1, public: BString(); // create an empty string BString(const char* c); // conversion from C-string

27 bstring.h ~BString(); // destructor, since dynamic management BString(const BString &s); // copy constructor BString & operator=(const BString &s); BString & operator+=(const BString &s); int length() const; // return length of string private: char *str; // pointer to my dynamic array of chars int size; // size of the string // allocation will always be size+1 };

28 bstring.cpp #include <cstring> #include "bstring.h" ostream &operator<<(ostream &os, const BString &s) { } istream &operator>>(istream &is, BString &s) { } istream &getline(istream& is, BString &s, char delim) {} bool operator<(const BString &s1, const BString &s2){ } bool operator>(const BString &s1,

29 bstring.cpp bool operator<=(const BString &s1, const BString &s2) { } bool operator>=(const BString &s1, bool operator==(const BString &s1, bool operator!=(const BString &s1,

30 bstring.cpp BString operator+(const BString &s1, const BString &s2) { } BString::BString() { size = 0; str = 0; } BString::BString(const char* c) { size = strlen(c); str = new char[size+1]; strcpy(str, c); // can use cstring functions, // as long as allocation is enough for '\0' }

31 bstring.cpp BString::~BString() { } BString::BString(const BString &s) { } BString &BString::operator=(const BString &s) { } BString &BString::operator+=(const BString &s) { } int BString::length() const { }

32 main.cpp #include <iostream> #include "bstring.h" using namespace std; int main() { BString s1; // should be empty string BString s2 = "Hello"; // should be "Hello" cout << "s1 = " << s1 << '\n'; cout << "s2 = " << s2 << '\n'; }

33 Exercise Fill in functions that are yet to be defined
Add test calls to the driver program


Download ppt "Strings: C-strings vs. Strings as Objects"

Similar presentations


Ads by Google