Lecture #27: More Strings
December
8, 1999
Bob
Pitha
so what else can we do with strings?
• Whatever
we want
– we just need
functions to do it
• As
far as C is concerned, strings are just arrays of chars
– they follow
all the same rules as other arrays
– the only
possibly exception is initialization
• C gives us a
convenient way to initialize a string
![Text Box: int iarray[] = {1,2,3,4,5};
char string[] = “abcde”;](./Lecture27_files/image001.gif)
but we need to do more
• Say
I asked you to write a program that let the user enter a date like this:
December 8, 1999
and it should tell me what day of
the week it is
– compared to
what we’ve done so far, this is a pretty complex problem
– we don’t
have to figure this out right now, but let’s look at it a little
our date program
• Given
a date like December 8, 1999
– first we
have to divide it into pieces
• month
(December)
• date (8)
• year (1999)
– that, in
itself, is a challenge
• the month
name is everything up to the first space
• the date is
whatever comes next, up to the comma
– but if it’s
not numerals, we have an error
• the year
comes last
so what do we do?
• Given
a string containing the date, we must:
– extract the
month name, probably into another string
– convert the
numerals of the date and year from text into numbers
• Then
once we have the month name, we have to figure out:
– whether it’s
valid
– and if so,
what month it is
parsing the string
• Breaking
the string down into its parts is sometimes called parsing
– it’s
sometimes a very complex task
– this is
pretty straightforward as text parsing goes, but it’s still pretty complex
compared to what we’ve been doing
– part of that
complexity comes from the fact that we can’t assume someone’s entered the date
correctly
• we have to
deal with things like:
– Dec ember 8,
1999
– December
8 1999
– December8,
1999
• at least to
recognize that they’re incorrect
string operations
• So
what sorts of things would we have to do?
– scan a
string for patterns
• to find the
month name, date, and year
– copy out
parts of the string into separate strings
• so we can
convert the date and year into numbers
• and so we
can process the month name
– compare
strings to other strings to recognize words
• to figure
out which month the month name is
– convert
digits into numbers
But…
• None
of these operations are part of the C language!
– remember, C
just thinks of strings as any other array
• just a way
to handle a bunch of values of the same type
– these
operations all involve processing the contents of the string
– these are
all done using useful string functions
string functions
• C
comes complete with a whole set of useful functions
– we’ve
already met printf and scanf
– there are a
lot more
– including a
bunch of useful string functions
let’s talk about a simple one…
• Very
often you need to find the length of a string
– remember
that a string is a bunch of chars lined up in memory with a null character at
the end
– so to find
the length of the string, we just need to count from the beginning until we
reach the end.
example
![Text Box: #include <stdio.h>
int main (void)
{
char string[80];
int length;
printf (“Enter a string: “);
scanf (“%s”, string);
length = strlen (string);
printf (“You entered %d characters.\n”, length);
return 0;
}](./Lecture27_files/image002.gif)
what does this do?
• This
is a very simple program, although it uses one small detail we haven’t talked
about

– the call to
scanf uses a control string with %s in it
– %s tells
scanf to read a string
• it will read
from the keyboard up to the first whitespace character
– whitespace
is a space, tab, or carriage return
• it expects a
corresponding pointer argument, which tells it where to store the string
• So
this program reads in a string, and prints its length
strlen()
• The
function strlen is one of the string functions profided in the C Standard
Library
– it takes a
single argument: a pointer to a string
– it returns
the length of that string
strlen()
• How
does it work?
– this
function looks at memory starting at the location specified by its argument
– it then
scans forward through memory, counting bytes until it finds a null character
what other functions are there?
• There
are a lot… some of the more useful are:
– strcpy –
copy the contents of one string into another
– strcat –
concatenate two strings (add one string onto the end of another)
– strcmp –
compare two strings
– strstr –
find a substring within a string
how might we use them?
• Here’s
an example…
– earlier we
talked about parsing a date and calculating the day of the week it falls on
– we’re not
going to get into how to figure out days of the week for arbitrary dates
• but let’s
take a specific piece of this problem…
• let’s look
at how we recognize the month name
• we need to
take a month name (i.e., “January”) and figure out which number month it is
– because the
algorithm for figuring out what day of the week it is requires the month as a
number
so how can we do this?
• What
we basically need to do is:
– compare the
entered month name with each month name that we know
– when we find
a match, we know which number it is.
– we have a
few different ways we can do this:
• I’ll show
you with an if statement, and a loop
example 1

example 2
![Text Box: int FindMonthNum (const char *monthName)
{
char *month[] = {“January”, “February”, “March”,
“April”, “May”, “June”, “July”, “August”,
“September”, “October”, “November”, “December”};
for (i = 0; i < numMonths; ++i)
if (strcmp (monthName, month[i]) == 0)
return i+1;
return 0;
}](./Lecture27_files/image005.gif)
those two examples…
• Either
of them would work
– but most
people would probably prefer the shorter one.