1/19/15
CS 280
Programming Language
Concepts
Programming Notes
Strings
1/19/15
Whats a string?
A sequence of characters that are grouped
together and dealt with as a single unit
Different languages handle strings in different
ways, and provide tools to help you deal with
strings in that language
OK, so first, whats a character?
A character (something of type char) contains a
sequence of 8 bits
By convention, the ASCII encoding for a character
is used
A character constant is a single character
enclosed in single quotes
The sequence a is a character constant for the
bit pattern that represents the letter a in ASCII
NOTE: more modern programs use UniCode
characters, but an 8-bit char predates UniCode
and is deeply ingrained into C-like languages
1/19/15
Other ways of representing chars
You can also put an octal or hex value inside
the single quotes, which represents that bit
pattern in a character
a and \141 and \x61 are all the same thing
There are so-called escape sequences which
represent common characters:
\n
\t
\
\\
\0
newline
tab
a single quote character
a backslash character
the null character (all zero bits)
Strings in C
A C-string is a sequence of characters with a null
character (\0) as an end marker
A C-string can be thought of as an array of
characters
Note that there is no string type; instead theres
just the convention of marking the end of an array
of chars with a null char (\0) and calling it a string
C has built in shorthand for these arrays
There are library routines that follow the
convention that a string is an array ending in \0
1/19/15
C shorthand for strings
This is a string:
{ h, e, l, p, , m, e, , r, h, o, n, d, a, !, \0 }
//thats an array initialization
So is this:
help me rhonda!
A double-quoted list of characters is a
shorthand for a string in C
The compiler automatically adds the null
character for you at the end of a quoted string
Variables for strings
A string is an array of characters, so
char mystring[7] = { h, e, l, l, o, !, \0 };
char mystring[7] = hello!;
char mystring[] = { h, e, l, l, o, !, \0 };
char mystring[] = hello!;
All of these are a declaration of an array of 7
characters, initialized to the string hello.
1/19/15
C String Libraries
The C standard library has functions that
follow the array of characters ending with a
null convention
For example
strlen(s) returns the length of the string in s
strcpy(to, from) copies the string in from into the string
in to
Some examples
char onestr[] = hello!;
char another[] = there;
char athird[100];
printf(%d\n, strlen(onestr) );
strcpy(athird, onestr); // copy into the array
printf(%s\n, onestr);
printf(%s\n, athird);
1/19/15
Error Examples
What if I tried to strcpy into something that isnt big
enough to fit what Im trying to copy?
bad things. very bad things.
onestr = another;
// << NO!!!!!
this is invalid C: strings are NOT basic types, you cant assign
them
both onestr and another are the names of arrays. You can
NOT assign to the name of an array
strcpy(another, onestr);
// << NO!!!!
this might break. on some systems, constants are read only
Additional string functions
strncpy strcpy with a length parameter
strcat concatenate strings
strcmp compare strings
strncmp compare strings, with a length
parameter
1/19/15
Assessment of C strings
Its pretty simple: all the string functions just
deal with arrays of characters
Not very efficient in some cases (for example,
strlen looks at every character in the string)
Might not be very safe
Strings in C++
Just about everything in C is in C++, therefore
everything weve discussed about strings in C is
true for C++
C++ ALSO has a definition for a string class;
basically a definition of a type for strings.
1/19/15
Example
#include <iostream>
#include <string>
using namespace std;
int
main()
{
string mystring = "hello!";
string yourstring = mystring;
cout << mystring.length() << '\n';
return 0;
}
Observations
A C++ string (a.k.a. std::string) can be
initialized from a quoted string
You can initialize one string from another
using assignment
The string keeps track of and manages
memory
Length does not require looking at the entire
string to count characters
You can concatenate strings with +
1/19/15
More string stuff
The relational operators work on C++ strings
string1 == string2 is valid in C++ strings
The i-th character in a C string is referenced
using array operations: mystring[i] is the i-th
character
That works on a C++ string, too
Theres also an at function that does the
same thing for C++ strings: mystring.at(i)
The C++ version does bounds checking
Compare this to Javas charAt() method
And in Java?
There is a String object (java.lang.String)
defined
Similar things can be done in Java: initialize
from a quoted string, copy values
The names of the string functions are
different
1/19/15
Concluding observations
Three approaches to the same problem
Two approaches appear in C++
The concepts are similar but the
implementations are different
Annoyingly, the function names are different
Observations we will come back to:
C++ has a way to make operators work on objects
Memory is important
10