CIS 22B - Notes for Wednesday, 10/17

Announcements and Reminders


C-Style string

The C language uses the term string to refer to null-terminated char arrays.   In the C language they are used to store character data.  In C++ we have the string class.

For example, consider the literal constant string, "hello" 
It occupies 6 bytes of memory. Its type is const char*
Memory "looks" like this:  

  h   e    l   l   o  \0

What is a null-terminated char array?

To assign this literal constant to a variable ...

char array[6];
strcpy(array,"hello");    // copies the literal into the memory location, array
// array = "hello";       // ERROR!!! This will not work.  Why not?

C-Style string functions

The C-style string functions operate on C-style strings (aka null terminated char array).
These function require the <cstring> header file.

Function Prototype Description
strcpy char* strcpy(char* destination, const char* source); Copies from the source string to the destination string
strncpy char* strncpy(char* destination, const char* source, size_t n); Copies n bytes from the source string to the destination string
strcat char* strcat(char* destination, const char* source); Appends the source to the end of the destination
strncat char* strncat(char* destination, const char* source, size_t n); Appends n bytes from the source to the end of the destination
strcmp int strcmp(const char* s1, const char* s2); Compares s1 and s2.  
Returns 0 if equal, negative int if s1 < s2, postive int if s1 > s2
strncmp int strncmp(const char* s1, const char* s2, size_t n); Compares n bytes of s1 and s2.
strstr const char* strstr(const char* s1, const char* s2);
char* strstr(char* s1, const char* s2);
Searchs string s1 for string s2
strchr const char* strchr(const char* str, int ch);
char* strchr(char* str, int ch);
Searches string for a character
strrchr const char* strrchr(const char* str, int ch);
char* strrchr(char* str, int ch);
Searches string for a character starting at the end of the string
strlen size_t strlen(const char* str); Returns the length of the string
strtok char* strtok(char* str, const char* delimiterset); Parses string str using any character contained in delimiterset

Note: Microsoft Visual Studio compilers will issues errors when compiling some C-string functions. To avoid this, insert this line at the top of your program:





For the file shown below, print the results shown below.  Use only <cstring> or <cctype> functions for the parsing.

Input File
Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16

Output File
Cincinnati over Buffalo 27 to 24
Detroit over Cleveland 31 to 17
Kansas City over Oakland 24 to 7
Carolina over Minnesota 35 to 10
Pittsburgh over NY Jets 19 to 6
Philadelphia over Tampa Bay 31 to 20
Green Bay over Baltimore 19 to 17
St. Louis over Houston 38 to 13
Denver over Jacksonville 35 to 19
Seattle over Tennessee 20 to 13
New England over New Orleans 30 to 27
San Francisco over Arizona 32 to 20
Dallas over Washington 31 to 16

The code

Character Type Functions


Read me