Sas14 Bes043
Sas14 Bes043
Productivity Tip: When handling a lot of data, it’s better to group them by name and purpose!
A. LESSON PREVIEW/REVIEW
1) Introduction (2 mins)
Topic: C Strings
Lesson connection and relevance to the students: Computers can manipulate numbers but
sometimes it’s fun if we can let the computers manipulate characters or strings also.
Learning targets: C String functions.
2) Activity 1: What I Know Chart, part 1 (3 mins)
Write in the first column what you know about the given questions in the second column below.
The third column is left blank at this time.
B. MAIN LESSON
1) Activity 2: Content Notes (13 mins)
Figure 11.13 Physical layout of a C string
C Strings
A string is a sequence of characters. C and C++ implement strings as arrays of char. The C++ language
additionally supports string objects. In the C language, the only option is a char array. We use the term C string
to refer to an array of characters as used in the C language. In this section, any mention of the term string refers
to a C string.
A string is an array of characters. A string literal is a sequence of characters enclosed within quotation
marks, as in
cout << "Howdy!\n";
All proper C strings are null terminated. This means the last character in the array is ASCII zero, which
C++ represents by the character literal '\0'. Figure 11.13 shows the physical layout of the string "Howdy!" in
memory.
Since strings are actually arrays, care must be taken when using string variables:
• Enough space must be reserved for number of characters in the string, including the null terminating
character.
• The array of characters must be properly null terminated.
The following code fragment is safe and acceptable:
char *word = "Howdy!";
cout << word << '\n';
The variable word is declared to be a pointer to a character, and it is initialized to point to a string literal.
The following code fragment is less safe:
char word[256];
cin >> word;
The string word can hold 255 viable characters plus the null terminator. If the user types in relatively short
words (length less than 255 characters), there is no problem. If at any time the user types in more characters
than will fit in the word array, the executing program will have a problem. The problem is known as a buffer
overrun. In the best case, buffer overruns lead to buggy programs. In the worst case, clever users can exploit
buffer overruns to compromise software systems. Buffer overruns are always logic errors and you should take
great care to avoid them.
The following code provides a safe way to get user input:
char word[10];
fgets(word, 10, stdin);
cout << word << '\n';
The fgets function is a standard C function. The second parameter specifies the maximum length of the
string, including the terminating null character, that will be
placed in the string word. The last argument, stdin is a C
construct related to the C++ object std::cin. In order to use
fgets within a program you must include the <cstdio>
header.
The following code begs for disaster:
char *word;
cin >> word;
In this case word points to a random location in
memory (it is uninitialized), and the code allocates no buffer
to receive the input characters from std::cin. The program’s
behavior executing this code is undefined, but it likely will
lead to the program crashing. Insidiously, depending on
how the operating system manages memory, the program
may run fine much of the time and crash only rarely.
Regardless, the program contains a serious bug.
When passing an array to a function a caller must
provide the size of the array so that the function may
process the array properly. Since C strings are null
terminated, such size information is not necessary. The
find_char function in Listing 11.21 (findchar.cpp) determines if a particular character is present in a string.
• int strncmp(const char *s, const char *t, int n) compares the first n characters of two strings for
lexicographic (dictionary) ordering. The function returns an integer less than zero if the first n characters of s
appear lexicographically before the first n characters of t; that is, s would appear before t is a dictionary. The
function returns an integer greater than zero if the first n characters of s appear lexicographically after the first n
characters of t. The function returns zero if the first n characters of the two strings are identical.
2) Activity 3: Skill-building Activities (with answer key) (18 mins + 2 mins checking)
1. Write a simple password program. The password is “Hi”. The program will only check the password
once. It will display “Password granted!” if the password is correct or else it will display “Password
denied!”. Use any C string functions available.
2. Upgrade the program in the number 1. The password program will have three trials in entering the
password if it is wrong. If the password has been entered incorrectly the first and second time, it will
display “Wrong password! Try again…”.
To develop habits on thinking about learning, answer the questions below about your learning
experience.
Three things you learned:
1.
2.
3.
2.) Since a C string uses a pointer, can the reference variable be applicable to C string data?
Answer: Yes.