How Long is a (Piece of) String?
Working with text is fairly common,
and in C, we treat text as
an array of characters,
where the last character
is followed by the character \0
,
which we refer to as the null terminator.
For this activity,
you’ll be writing the function stringLength
.
It takes a string,
and finds its length,
excluding the null-terminator.
Download
stringLength.c
,
or copy it into your current directory on a CSE system by running
$ cp /web/cs1511/17s2/week05/files/stringLength.c .
We’ve provided some simple, assert
-based tests
to help you build your solution:
assert (stringLength ("") == 0);
assert (stringLength ("!") == 1);
assert (stringLength ("Hello, world!") == 13);
assert (stringLength ("17... seventeen.\n") == 17);
You should probably write more tests to demonstrate your solution works.
To run some simple automated tests:
$ 1511 autotest stringLength
To run Styl-o-matic:
$ 1511 stylomatic stringLength.c Looks good!
You’ll get advice if you need to make changes to your code.
Submit your work with the give command, like so:
$ give cs1511 wk05_stringLength
Or, if you are working from home, upload the relevant file(s) to the wk05_stringLength activity on Give Online.