Previous: 4.8 Exercises
Up: 4 Processing Character Data
Previous Page: 4.8 Exercises
- First use graph paper to plan out and then write a program that prints
the following message centered within a box whose borders are made up of the
character *.
Happy New Year
- Write a program to print a character corresponding to an ASCII value or
vice versa, as specified by the user, until the user quits. If the character is
not printable, print a message.
- Write a function that takes one character argument and returns the
following: if the argument is a letter, it returns the position of the letter
in the alphabet; otherwise, it returns FAIL,
whose value is -1. For example, if
the argument is 'A',
it returns 0; if the argument is 'd', it returns 3, and so
forth. Define and use macros to test if a character is a lower case letter or
an upper case letter.
- Use a switch statement to write a function that returns TRUE if a
character is a consonant and returns FALSE otherwise.
- Use a switch statement to write a function that returns TRUE if a digit
character represents an odd digit value. If the character is not an odd digit,
the function returns FALSE.
- Write a program to count the occurrence of a specified character in the
input stream.
- Write a program that reads in characters until end of file. The program
should count and print the number of characters, printable characters, vowels,
digits, and consonants in the input. Use functions to check whether a character
is a vowel, a consonant, or a printable character. Define and use macros to
test if a character is a digit or a letter.
- Modify the program in Chapter
to find prime numbers so that the inner
loop is terminated by a break
statement when a number is found not to be prime.
- Write a function that takes two arguments, replicate(int n, char c);,
and prints the character, c, a number, n, times.
Use replicate() to write a function,
drawrect(), that draws a rectangle of
length, g, and width, w.
The dimensions are in terms of character spaces. The
rectangle top left corner is at top, t, and left, l.
The arguments, g, w, t, and l
are integers, where t and l
determine the top left corner of the rectangle, and
the length of the rectangle should be along the horizontal.
Use '*' to draw
your lines. Write a program that repeatedly draws rectangles until length and
width specified by the user are both zero.
- Repeat 10,
but modify drawrect() to fillrect() that draws a rectangle
filled in with a specified fill character.
- Write a function that draws a horizontal line proportional to a specified
integer between the values of 0 and 50. Use the function in a program to draw a
bar chart, where the bars are horizontal and in proportion to a sequence of
numbers read.
Write a function to encode text as follows:
- a.
-
If the first character of a line is an upper case letter, then
encode the first character to one that is 1 position higher in a circular
alphabet. Move the rest of the characters in the line up by 1 position in a
circular printable part of the ASCII character set.
- b.
-
If the first character of a line is a lower case letter, then move
the first character down by 2 positions in a circular alphabet. Move the rest
of the characters in the line down by 2 positions in a circular printable part
of the ASCII character set.
- c.
-
If the first character of a line is white space, then terminate
the input.
- d.
-
Otherwise, if the first character of a line is not a letter,
then move all characters in the line down by 1 position in a circular printable
part of the ASCII character set.
Write a function to decode text that was encoded as per Problem 13.
- Write a menu-driven program that combines Problems
13 and 14 to encode or
decode text as required by the user. The input for encoding or decoding is
terminated when the first character of a line is a space.
The commands are: encode, decode, help, and quit.
Write a function that takes three arguments, two float numbers and one
arithmetic operator character. It returns the result of applying the operator
to the two numbers. Using the function, write a program that repeatedly reads a
float number, followed by an arithmetic operator, followed by a float
number; each time it prints out the result of applying the operator to the
numbers.
- Modify the program in Problem 16
to allow further inputs of a sequence of
an operator followed by a number. Each new operator is to be applied to the
result from the previous operation and the new number entered. The input is
terminated by a newline.
Print only the final result.
- Read and convert a sequence of digits to its equivalent integer. Any
leading white space should be skipped. The conversion should include digit
characters until a non-digit character is encountered. Modify the program so it
can read and convert a sequence of digit characters preceded by a sign,
+ or -.
- Write a program that converts the input sequence of digit characters,
possibly followed by a decimal point, followed by a sequence of digits, to a
float number. The leading white space is skipped and the input is
terminated when a character not admissible in a float number is encountered.
- Modify the above program to include a possible leading sign character.
- Write a function that takes a possibly signed integer as an argument, and
converts it to a sequence of characters.
- Write a program that takes a possibly signed floating point number and
converts it to a sequence of characters with 4 digits after the decimal point.
- Modify the word extraction program, wds.c,
in Figure 4.16. It should
count words with exactly four characters and words with five characters. Assume
the input consists of only valid characters and white space.
Write a program that reads in characters until end of file. The program
should identify each token, i.e. a word after skipping white space.
The only valid token types are: integer and invalid.
White space delimits words but is otherwise ignored.
An integer token is a word that
starts with a digit and is followed by digits and
terminates when a non-digit character is encountered.
An invalid token is made up of any other single character
that does not belong to an integer.
Print each token as it is encountered as well as its type.
Here is a sample session:
- Type text, EOF to quit: 3456 a23b
- 3456 integer
- a invalid
- 23 integer
- b invalid
- Type text, EOF to quit: '136D
Modify the program in Problem 24
so it also allows an identifier as a
valid token. An identifier starts with a letter and may be followed by a
sequence of letters and/or digits.
- Modify the program in Problem 25
so that tokens representing float numbers
are also allowed. A float token must start with a digit, may be followed by a
sequence of digits, followed by a decimal point, followed by zero or more
digits.
Here is a sample session:
- Type text, EOF to quit: The ID Number is 123, not 123.
- The Identifier
- ID Identifier
- Number Identifier
- is Identifier
- 123 Integer
- , Invalid
- not Identifier
- 123. Float
- Type text, EOF to quit: pay = 1.5 * hours * rate;
- pay Identifier
- = Invalid
- 1.5 Float
- * Invalid
- hours Identifier
- * Invalid
- rate Identifier
- ; Invalid
- Type text, EOF to quit: '136D
Hint: Skip leading delimiters; test the first non-delimiter, and build a word
of the appropriate type. An integer and a float are distinguished by the
presence of a decimal point.
Previous: 4.8 Exercises
Up: 4 Processing Character Data
Previous Page: 4.8 Exercises