CS 102 (Fall '03)
[Schedule] [Examples] [Programs] [Notes & Reference] [Syllabus] [Lab & TA] [Tests] [Grades]

Strings

Motivation, Examples, Pointers, String Functions, Online Doc

Motivation

We often need to manipulate stored text in C. This can be done using arrays of characters, which are called strings.

Examples reading in strings

Reading multiple words into strings:

  1. Reading character by character, simply echoing the input
  2. Reading character by character, storing each word into an array
  3. Reading a word at a time into an array

arrayStrings.c: using arrays to store strings, reading with %s, overwriting the '\0' at the end of a string. (See also a version tailored for the EECS machines)

encode.c: encoding some input text by a set transposition amount

A Brief Introduction to Pointers

See in-class notes

arrayptr1.cpp: An example using a pointer with an array

Examples of String Functions

Doing a word lookup in a 2-D array of words, using our own function to compare strings. Same thing, only now a version using strcmp instead of our own function.

A description of some of the string functions.

stringFcns.cpp: A program using various string functions: strcat, strcmp, strcpy, strlen, strstr, strchr, strncpy

Reading a line at a time into an array, then using strtok to parse out each word from the array. (You need to know a bit about pointers to understand this). To understand how strtok works, look at this program that does a similar thing, except we've written the function ourselves.

On-line documentation

See string.h in the C standard library functions.


[CS Dept] [UIC] [Prof. Reed]