Week 3: Exercises

1. AEIOU

Write a program that reads a string on a single line and prints "vowels" if the string contains any vowels, otherwise "no vowels".

2. Reverse It

Read a string and print it backwards.

Enter string: zyzzyva
avyzzyz

3. Spaces In Between

Read a string and write it out with a space between each adjacent pair of characters.

Enter string: waffle
w a f f l e

4. Double Or Nothing

Read a string and print 'double' if any two adjacent characters are the same, or 'nothing' otherwise.

Enter string: abacuses
nothing
===
Enter string: lionesses
double

5. Random Card

Each card in a standard deck has a rank (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Ace) and a suit (clubs, diamonds, hearts, or spades).

Write a program that prints a random card such as "9 of hearts" or "king of spades".

6. Lottery Ticket

A country is holding a lottery in which 5 winning numbers are chosen at random from the range 1..25. No two of these numbers will be the same. For example, the winning numbers could be 2 – 4 – 9 – 18 – 22 or 5 – 6 – 11 – 15 – 24.

Write a program that randomly selects 5 winning numbers and prints them on a single line.

7. Password Generator

Write a program that generates a random password with 10 lowercase letters. The password should contain alternating consonants and vowels.

Sample outputs:

  kimolonapo
  ritilenora

8. S Begins With T

Write a program that reads two strings S and T, each on its own line. The program should print "true" if S starts with T, "false" otherwise.

9. String Comparison

Write a program that reads two string S and T, each on its own line. The program should print the string that is lexicographically greater, i.e. the string that appears later in dictionary order. Do not use the built-in string comparison operator '>'.

Enter S: limerick
Enter T: limeade
limerick

10. 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

11. 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

12. Letter Histogram

Write a program that reads lines of text until the user presses Ctrl+D (or Ctrl+Z on Windows) and determines how many times each letter A-Z appears in the input. 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 print the most frequent letter with a count of its occurrences. It should 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: **

...

13. Printing with Commas

Write a program that reads an integer which may be arbitrarily large, and writes the integer with embedded commas.

Input:

28470562348756298345

Output:

28,470,562,348,756,298,345