Week 2: Exercises

These are optional exercises that I suggest you try to practice the subset of Pascal we've learned so far.

1. All Upper

Write a program that reads a string and makes all lowercase letters uppercase without using the library function upCase.

Sample input:

100 Spires and Towers

Sample output:

100 SPIRES AND TOWERS

2. Letters To Digits To Letters

Write a program that reads a string and converts all uppercase letters from A, B, C, ... J into the digits 1, 2, 3, …, 0, and also converts the digits 1, 2, 3, …, 0 back into A, B, C, …, J.

Sample input:

ABC123J

Sample output:

123ABC0

3. Sum Square Difference

Solve Project Euler's problem 6.

4. A Tee

Write a program that reads an odd integer N. If the integer is even, prompt the user to reenter it. Use asterisks to draw a letter T with thickness 3, height N and width N. For example:

Enter N: 6
N must be odd.
Enter N: 7

*******
*******
*******
  ***
  ***
  ***
  ***
  

5. Most Frequent Letter

Write a program that reads a single line of input text, and determines which letter from A to Z appears most frequently in the input text. You should ignore case, considering 'a' and 'A' to be the same letter. The input text is guaranteed to contain at least one letter, and will not contain any accented characters such as 'ř'.

The program should output a single line containing the most frequent letter in uppercase, followed by a space and then the number of occurrences of that letter.

Sample input:

The quick brown fox jumped over the lazy dog.

Sample output:

E 4

6. Letter Histogram

Modify your program from exercise (4) above in two ways.

First, the program should read lines of text until the user presses Ctrl+D (or Ctrl+Z on Windows), not just a single line.

Second, the program should print the most frequent letter and also print a histogram showing how many times each letter occurred in the input. If a letter occurs 1-3 times, print a single *. If it occurs 4-6 times, print two asterisks (**), and so on.

Sample input:

The quick fox jumped over the lazy dog.
Then the dog got up and jumped over the fox.

Sample output:

e 9

a: *
b: 
c: *
d: **
e: ***
f: *
g: *
h: **

...

7. Random X's

Write a program that reads an integer N, and writes out a 10 x 10 block of dots (.), with X's in exactly N random positions.

Sample input:

10

Sample output:

. . . . . . . . . . 

. . . . . . . . . . 

. . . . . . . . . . 

. . . . X . . . . . 

. . X . . . X . . . 

. . . . . X . . X . 

. . . X . . . . X . 

. . . . . . . . X . 

X . . . . . . . . . 

. . X . . . . . . . 

8. One Big X

Write a program that reads an odd integer N. If the integer is even, prompt the user to reenter it. Use asterisks to draw a letter X with thickness 3, height N and width (N + 2). For example:

Enter N: 8
N must be odd.
Enter N: 9

***     ***
 ***   ***
  *** ***
   *****
    ***
   *****
  *** ***
 ***   ***
***     ***

Hint: One way to implement this is to allocate a two-dimensional array of booleans and set the array elements where the asterisks should be to true. Or you could skip the array and do some calculations as you write out each line.

9. Largest product in a series

Solve Project Euler's problem 8.

Hint: Manually copy and paste the 1000-digit number into a text file called big_number. You can run your program from the command line like this:

$ ./myProgram < big_number

This will redirect input to your program from this file. So your program can read the number from standard input using ordinary readln() calls.