Programming Projects require more problem-solving than Practice Programs and can usually be solved many different ways. Visit www.myprogramminglab.com to complete many of these Programming Projects online and get instant feedback.
Write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all strings of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Do not worry about proper names; if their first letters are changed to lowercase, that is acceptable. Treat a line break as if it were a blank, in the sense that a line break and any number of blanks are compressed to a single blank. Assume that the sentence ends with a period and contains no other periods. For example, the input
the Answer to life, the Universe, and everything
IS 42.
should produce the following output:
The answer to life, the universe, and everything is 42.
Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, and periods. When outputting the number of letters that occur in a line, be sure to count upper- and lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that do occur in the input line. For example, the input line
I say Hi.
should produce output similar to the following:
3 words
1 a
1 h
2 i
1 s
1 y
Give the function definition for the function with the following function declaration. Embed your definition in a suitable test program.
void getDouble(double& inputNumber);
//Postcondition: inputNumber is given a value
//that the user approves of.
You can assume that the user types in the input in normal everyday notation, such as 23.789, and does not use e-notation to type in the number. Model your definition after the definition of the function getInt
given in Display 8.3 so that your function reads the input as characters, edits the string of characters, and converts the resulting string to a number of type double
. You will need to define a function like readAndClean
that is more sophisticated than the one in Display 8.2, since it must cope with the decimal point. This is a fairly easy project. For a more difficult project, allow the user to enter the number in either the normal everyday notation, as discussed above, or in e-notation. Your function should decide whether or not the input is in e-notation by reading the input, not by asking the user whether she or he will use e-notation.
Write a program that reads a person’s name in the following format: first name, then middle name or initial, and then last name. The program then outputs the name in the following format:
lastName, firstName Middle_Initial.
For example, the input
Mary Average User
should produce the output:
User, Mary A.
The input
Mary A. User
should also produce the output:
User, Mary A.
Your program should work the same and place a period after the middle initial even if the input did not contain a period. Your program should allow for users who give no middle name or middle initial. In that case, the output, of course, contains no middle name or initial. For example, the input
Mary User
should produce the output
User, Mary
If you are using C strings, assume that each name is at most 20 characters long. Alternatively, use the class string
.
(Hint: You may want to use three string variables rather than one large string variable for the input. You may find it easier to not use
getline
.)
Write a program that reads in a line of text and replaces all four-letter words with the word “love
”. For example, the input string
I hate you, you dodo!
should produce the output
I love you, you love!
Of course, the output will not always make sense. For example, the input string
John will run home.
should produce the output
Love love run love.
If the four-letter word starts with a capital letter, it should be replaced by "Love"
, not by "love"
. You need not check capitalization, except for the first letter of a word. A word is any string consisting of the letters of the alphabet and delimited at each end by a blank, the end of the line, or any other character that is not a letter. Your program should repeat this action until the user says to quit.
Write a program that reads in a line of text and outputs the line with all the digits in all integer numbers replaced with 'x'
. For example,
Input:
My userID is john17 and my 4 digit pin is 1234 which is secret.
Output:
My userID is john17 and my x digit pin is xxxx which is secret.
Note that if a digit is part of a word, then the digit is not changed to an 'x'
. For example, note that john17
is NOT changed to johnxx
. Include a loop that allows the user to repeat this calculation again until the user says she or he wants to end the program.
Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace "he"
with "she or he"
. Thus, the input sentence
See an adviser, talk to him, and listen to him.
should produce the following suggested changed version of the sentence:
See an adviser, talk to her or him, and listen to her or him.
Be sure to preserve uppercase letters for the first word of the sentence. The pronoun
"his"
can be replaced by"her (s)"
; your program need not decide between"her"
and"hers"
. Allow the user to repeat this for more sentences until the user says she or he is done.
This will be a long program that requires a good deal of patience. Your program should not replace the string "he"
when it occurs inside another word, such as "here"
. A word is any string consisting of the letters of the alphabet and delimited at each end by a blank, the end of the line, or any other character that is not a letter. Allow your sentences to be up to 100 characters long.
Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints
rather than an array. This function will not need a parameter like numberUsed
as in Display 7.12, since a vector can determine the number used with the member function size()
. This sort function will have only this one parameter, which will be of a vector type. Use the selection sort algorithm (which was used in Display 7.12).
Redo Programming Project 6 from Chapter 7, but this time use vectors instead of arrays. (It may help to do the previous Programming Project first.)
Redo Programming Project 5 from Chapter 7, but this time use vectors instead of arrays. You should do either Programming Project 8 or 9 before doing this one. However, you will need to write your own (similar) sorting code for this project rather than using the sorting function from Programming Project 7 or 8 with no changes.
Your country is at war and your enemies are using a secret code to communicate with each other. You have managed to intercept a message that reads as follows:
:mmZ\dxZmx]Zpgy
The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 shows the ASCII character set. Individual characters in a string are encoded using this system. For example, the letter “A” is encoded using the number 65 and “B” is encoded using the number 66.
Your enemy’s secret code takes each letter of the message and encrypts it as follows:
if (originalChar + key > 126) then
encryptedChar = 32 + ((originalChar + key) − 127)
else
encryptedChar = (originalChar + key)
For example, if the enemy uses key = 10 then the message “Hey” would be encrypted as:
Character | ASCII code |
---|---|
H |
72 |
e |
101 |
y |
121 |
Encrypted H = (72 + 10) = 82 = R in ASCII
Encrypted e = (101 + 10) = 111 = o in ASCII
Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$.”
Write a program that decrypts the intercepted message. The ASCII codes for the unencrypted message are limited to the visible ASCII characters. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.
Write a program that inputs a time from the console. The time should be in the format "HH:MM AM"
or "HH:MM PM"
. Hours may be one or two digits, for example, "1:10 AM"
or "11:30 PM"
. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four-digit military time based on a 24-hour clock. For example, "1:10 AM"
would output "0110 hours"
, "11:30 PM"
would output "2330 hours"
, and "12:15 AM"
would output "0015 hours"
. The function may either write the time to the console or return a string to be written to the console by the main
function.
The XML (eXtensible Markup Language) is a common format used to structure and store data on the Web. The following is a small sample XML file that could be used to store names in an address book. Type it in using a text editor and save it to a file named address.xml
(or find it at the accompanying website).
<?xml version="1.0"?>
<address_book>
<contact>
<name>George Clooney</name>
<street>1042 El Camino Real</street>
<city>Beverly Hills</city>
<state>CA</state>
<zip>90214</zip>
</contact>
<contact>
<name>Cathy Pearl</name>
<street>405 A St.</street>
<city>Palmdale</city>
<state>CA</state>
<zip>93352</zip>
</contact>
<contact>
<name>Paris Hilton</name>
<street>200 S. Elm St.</street>
<city>Beverly Hills</city>
<state>CA</state>
<zip>90212</zip>
</contact>
<contact>
<name>Wendy Jones</name>
<street>982 Boundary Ave.</street>
<city>Palmdale</city>
<state>CA</state>
<zip>93354</zip>
</contact>
</address_book>
The sample file contains four contacts. The <>
tag denotes the start of a field and the </>
tag denotes the end of the field.
You are hosting a party in Palmdale, CA. Write a program that reads in the address.xml
file and outputs the names and addresses of everyone in Palmdale. Your program shouldn’t output any of the tag information, just the address content.
You would like to send an advertising flyer to everyone in zip codes 90210 through 90214. Write a program that reads in the address.xml
file and outputs the names and addresses of everyone whose zip code falls within the specified range.
You may assume that each contact in the address file has the same structure and the same fields. However, your solution should be able to handle an input file with any number of contacts and should not assume that the fields within each contact are in the same order.
Given the following header:
vector<string> split(string target, string delimiter);
implement the function split
so that it returns a vector of the strings in target
that are separated by the string delimiter
. For example:
split("10,20,30", ",")
should return a vector with the strings "10"
, "20"
, and "30"
. Similarly,
split("do re mi fa so la ti do", " ")
should return a vector with the strings "do"
, "re"
, "mi"
, "fa"
, "so"
, "la"
, "ti"
, and "do"
.
Write a function that determines if two strings are anagrams. The function should not be case sensitive and should disregard any punctuation or spaces. Two strings are anagrams if the letters can be rearranged to form each other. For example, “Eleven plus two” is an anagram of “Twelve plus one.” Each string contains one “v”, three “e’s”, two “l’s”, etc. Test your function with several strings that are anagrams and non-anagrams. You may use either the string
class or a C-style string.
In many races competitors wear an RFID tag on their shoe or bib. When the racer crosses a sensor a computer logs the racer’s number along with the current time. Sensors can be placed along the course to accurately calculate the racer’s finish time or pace and also to verify that the racer crossed key checkpoints. Consider such a system in use for a half marathon running race, which is 13.1 miles. In this problem there are only three sensors: at the start, at the 7 mile point, and at the finish line.
Here is sample data for three racers. The first line is the gun time in the 24 hour time format (HH MM SS). The gun time is when the race begins. Subsequent lines are recorded by sensors and contain the sensor ID (0=start, 1=midpoint, 2=finish) followed by the racer’s number followed by the time stamp. The start time may be different than the gun time because sometimes it takes a racer a little while to get to the starting line when there is a large pack.
08 00 00
0,100,08 00 00
0,132,08 00 03
0,182,08 00 15
1,100,08 50 46
1,182,08 51 15
1,132,08 51 18
2,132,09 34 16
2,100,09 35 10
2,182,09 45 15
Create a text file with a sample race log. Write a program that reads the log data into array(s) or vector(s). The program should then allow a user to enter a racer’s number and it should output the racer’s overall finish place, race split times in minutes/mile for each split (i.e., the time between sensors), and the overall race time and overall race pace.
For a more challenging version modify your program so that it works with an arbitrary number of sensors placed at different locations along the course instead of just 3 locations. You will need to specify the mile marker for each sensor.
Based on the log file described in Programming Project 16 write a program to detect cheating. This could occur if:
A racer misses a sensor, which is a sign that the racer may have taken a shortcut.
A race split is suspiciously fast, which is a sign that the racer may have hopped in a vehicle. In this case, a race split faster than 4:30 per mile can be considered suspicious.
The output should be a list of suspected cheaters along with the reason for suspicion.
Write a program that inputs two strings (either C-string or STL string) that represents a time of day using the format HH:MM:SS AM|PM and then outputs the time elapsed from the first to the second time in minutes and seconds.
For example, given the strings:
11:58:10 PM
12:02:15 AM
The program should output that the time elapsed is 4 minutes and 5 seconds.
Write a program that manages a list of up to 10 players and their high scores in the computer’s memory. Use two arrays to manage the list. One array should store the player’s name and the other array should store the player’s high score. Use the index of the arrays to correlate the name with the score. In Chapters 10 and 11, you will learn a different way to organize related data by putting them into a struct or class. Your program should support the following features:
Add a new player and score. If it is one of the top 10 scores then add it to the list of scores. The same name and score can appear multiple times. For example, if Bill played 3 times and scored 100, 100, and 99, and Bob played once and scored 50, then the top scores would be Bill 100, Bill 100, Bill 99, Bob 50.
Print the top 10 names and scores to the screen sorted by score with the highest score first.
Allow the user to enter a player name and output that player’s highest score if it is on the top 10 list or a message if the player’s name has not been input or is not in the top 10.
Allow the user to enter a player name and remove the highest score for that player from the list.
Create a menu system that allows the user to select which option to invoke.