BD Software delivers on-site C++ training, Unix training and Java training.

BD Software delivers on-site training seminars for programmers in C, C++, Java, Perl and Unix

C++ Pretest
Prerequisites and Warm-Up Exercise

Prerequisites:

During the course, you will do about a dozen programming exercises. Therefore, attendees should have some programming experience with C. (A reading knowledge is rarely sufficient.) In particular, you should come to the course with the ability to:

  • read and write data declarations and function prototype declarations
  • understand the interchangeability of pointer and subscripting notation
  • work with structs and pointers to structs
  • build programs from more than one source (.c) file

Warm-Up Exercise:

The purpose of this exercise is to get you to review programming in C in preparation for learning C++. The exercise also provides useful background for the case study presented in the first day of the C++ course.

Assignment:

Write a function int getword(char *word, int n); that reads a 'word' from a document via standard input.

Here, a word is:

1. any sequence of letters and digits starting with a letter, or

2. a single non-space character, or

3. a newline ( '\n' ).

getword skips over space characters (except newline) between words.

By this definition, the line:

C++ and Object-Oriented Programming

contains 9 'words':

    • C
    • +
    • +
    • and
    • Object
    • -
    • Oriented
    • Programming
    • \n

getword copies the word it reads into the character array word, and places a null character '\0' after the last character copied into the array. getword assumes that word can hold at most n characters, including the null. If the current word in the input is too big to fit in the array, getword still reads the entire word and simply discards the excess characters.

If getword encounters EOF, it simply places a null in the first position of the array, and returns EOF. Otherwise, it returns the first character in the array.

Use getword to write a program that simultaneously counts the number of words in the input of length 1, length 2, etc., up to some length N. The last total reflects the number of words of length N or more. N should be a symbolic constant in the program, set by a #define.

If N is 10, the output should be something of the form:

    • 15 words of length 1
    • 46 words of length 2
    • 12 words of length 3
    • 6 words of length 10 or more

The program should only count words beginning with a letter or digit. For example, the program should count five words in the sample sentence:

    • C
    • and
    • Object
    • Oriented
    • Programming

and not count

    • +
    • +
    • -
    • \n

Hints:

1. Use getchar or fgetc to read the input. Don't read directly into the array. Get each character into an int variable first, and don't copy a character to the array until you're sure it's not EOF.

2. Use the is... functions in <ctype.h> to detect letters, digits, spaces, etc. Note that isspace includes a newline, so when skipping spaces, you need a loop roughly of the form

while ( c is a space and c is not a newline )

3. When reading to the end of a word, getword must read a non-letter or non-digit to know it has reached the end of that word. At that point, it's read one character too many. Use the library function ungetc to put that extra character back into the input so that the next call to getword will read it.


Back to Courses.

Back to C++ and OOP.

Solution to the Pretest.


Home | Courses | Tools | Resources
Clients | Successes | Contact | Site Map | Links | About Us

All text and images on this website are Copyright © 2001-2003 BD Software.
All rights reserved.


Courses:

C and C++ Seminars

Java Seminars

Unix-Related Seminars


Clients:

Lucent

Dell

Wal-Mart

Boston Technology

(See full
client list)


BD Software delivers on-site training seminars for programmers in C, C++, Java, Perl and Unix