Programming Language Concepts
Programming Language Concepts
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
1/19/15
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
So is this:
help me rhonda!
char mystring[7] = { h, e, l, l, o, !, \0 };
char mystring[7] = hello!;
char mystring[] = { h, e, l, l, o, !, \0 };
char mystring[] = 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!!!!
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
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