Andy Wang Object Oriented Programming in C COP 3330 Cstyle Strings Cstring is a nullterminated array of type char No builtin string type in C Must be char arrays NOT every char array is a Cstring only nullterminated ones ID: 561752
Download Presentation The PPT/PDF document "Strings: C-strings vs. Strings as Objec..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Strings: C-strings vs. Strings as Objects
Andy Wang
Object Oriented Programming in C++
COP 3330Slide2
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 onesSlide3
Library Features
C++ <
cstring
> library provides functions to work with C-style strings
strlen
() // string length
s
trcpy
(),
strncpy
() // string copy
s
trcat
(),
strncat
() // string concatenation
s
trcmp
(),
strncmp
() // string comparison
s
trstr
() // string search
s
trtok
() // string tokenizationSlide4
Some Implementation Details
i
nt
strlen
(
const
char *s) {
for (
int
j = 0; s[j] != ‘\0’;
j++
);
return j;
}
c
har *
strcpy
(char *s1,
const
char *s2) {
int
j = 0;
for (; s2[j] != ‘\0’;
j++
) s1[j] = s2[j];
s1[j] = s2[j];
return s1;
}Slide5
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]);
}
c
har *
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;
}Slide6
C++ <iostream
> Library
Provides I/O handling functions for C-style strings
c
har
str
[40];
c
out
<< str1; // insertion
c
in
>> str1; // extraction
c
in.get
(str1, 40, ‘,’); // reads to delimiter (comma)
c
in.getline
(str1, 40); // reads to delimiter (default is
endl
)
// discards delimiterSlide7
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 functionsSlide8
More Hurdles for Common Ops
c
har greeting[40];
// can’t do greeting = “Hello World”;
instead
s
trcpy
(greeting, “Hello World”);
// can’t do if (str1 == str2); instead
i
f (
strcmp
(str1, str2) == 0)
A C coder needs to dynamically resize c-stringsSlide9
C-String Pitfalls
C-string library functions often assume the use of ‘\0’ to terminate a string
What prints here?
c
har vowel[5] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};
c
out
<< 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”;
c
har welcome[10] = “Hello”;
s
trcpy
(welcome, greeting); // anything to worry about?Slide10
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?Slide11
Example
Non null terminating string
http://www.cs.fsu.edu/~
myers/cop3330/examples/strings/pitfalls/output.cppSlide12
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'};
Slide13
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
';
}Slide14
Example
Self overwrite
http://www.cs.fsu.edu/~
myers/cop3330/examples/strings/pitfalls/copy.cppSlide15
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
';
Slide16
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?
}Slide17
Example
Self concatenation
http://www.cs.fsu.edu/~myers/cop3330/examples/strings/pitfalls/concat.cppSlide18
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';
Slide19
concat.cpp
strcat
(buffer
, word2); // buffer is now "Dogfood"
cout
<< "buffer = " << buffer << '\n
';
strcat
(buffer, " breath");
// buffer is now
// "
Dogfood breath"
cout
<< "buffer = " << buffer << '\n
';
strcat
(buffer, buffer); // plenty of
room?
cout
<< "buffer = " << buffer << '\n
';
return 0;
}Slide20
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
referenceSlide21
Building a S
tring 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 concatenationSlide22
Building a S
tring 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, tooSlide23
Example: BString
class
http://www.cs.fsu.edu/~myers/cop3330/examples/strings/bstring/Slide24
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
');Slide25
bstring.h
friend
bool operator
<(
const
BString
&s1
,
const
BString
&s2
);
friend bool operator
>(
const
BString
&s1
,
const
BString
&s2
);
friend bool operator
<=(
const
BString
&s1
,
const
BString
&s2
);
friend bool operator
>=(
const
BString
&s1
,
const
BString
&s2
);
Slide26
bstring.h
friend
bool operator
==(
const
BString
&s1
,
const
BString
&s2
);
friend
bool operator
!=(
const
BString
&s1
,
const
BString
&s2);
friend
BString
operator
+(
const
BString
&s1
,
const
BString
&s2);
public:
BString
(); // create an empty string
BString
(
const
char* c);
//
conversion from
C-string
Slide27
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
};Slide28
bstring.cpp
#include <
cstring
>
#include "
bstring.h
"
o
stream
&operator
<<(
ostream
&
os
,
const
BString
&s) { }
i
stream
&operator
>>(
istream
&is
,
BString
&s) { }
i
stream
&
getline
(
istream
& is,
BString
&s,
char
delim
) {}
bool operator
<(
const
BString
&s1
,
const
BString
&s2){ }
bool operator
>(
const
BString
&s1
,
const
BString
&s2){ }Slide29
bstring.cpp
bool
operator
<=(
const
BString
&s1
,
const
BString
&s2
)
{ }
bool operator
>=(
const
BString
&s1
,
const
BString
&s2) { }
bool operator
==(
const
BString
&s1
,
const
BString
&s2) { }
bool operator
!=(
const
BString
&s1
,
const
BString
&s2) { }Slide30
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'
}Slide31
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
{ }Slide32
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
';
}Slide33
Exercise
Fill in functions that are yet to be defined
Add test calls to the driver program