Here are some practice problems for Exam 2.
Write a program that takes a command line argument and prints the uppercase version of each character in the argument. For example:
$ ./upper hello
HELLO
Use the function toupper()
to get the upper case version of a single character. For example, toupper('h')
returns 'H'
. If the character is already uppercase or the character is not a letter, the original character is returned. Include <ctype.h>
for this function.
Place the code that transforms the string into a function that modifies the string. Print the modified string in main()
.
Write a program which prints out the minimum and maximum values of a list of integers provided as command line arguments.
Do this by first building an array of integers from the command line arguments. You can use atoi()
(which stands for ASCII to integer) to convert each individual argument to an integer before placing it in an array. Include <stdlib.h>
for atoi()
. Assuming you have already declared an array named array to store the integers, you can do this:
for (int i = 1; i < argc; i++) {
array[i - 1] = atoi(argv[i]);
}
Note that array is going to have one fewer elements than argv
, since the first element of argv
is the name of the program itself.
Write a void function that calculates the min and max of an array of integers and “returns” the two values through pointer parameters. Pass your integer array to this function to get the min and max, and then print them out.
For example, if you run your program like this:
$ ./minmax 10 2 15 -3 14
it should print -3 and 15.
Try two additional ways of writing the function:
Write a program to determine if a string is a palindrome (a palindrome is a string that is the same when reversed, like “mom” and “racecar”). For the simplest version of this program, take the input as a command line argument.
For the command line string, remember that to use a string with spaces as one command line argument you need to use quotation marks:
$ ./palindrome "UFO tofu"
Your string is a palindrome!
Put the code to determine if a string is a palindrome into a function, so that you can call the function on either a command line argument or standard input.
BONUS: For a more complex version of the problem, expand your program so it will read input from standard input if a command line argument is not provided. Use a growable dynamically allocated array to store the string so that there is no limit to the size of the string. Since the last character from standard input will likely be '\n'
, ignore any non-alphabetic characters (hint: use isalpha()
). If you also convert all the letters to lowercase, strings like the following will be considered palindromes:
A man, a plan, a canal, Panama.
Go hang a salami, I'm a lasagna hog!
Write a program that determines which are the highest and lowest paid employees are the lowest and paid. Each employee will be represented with a struct that has four fields:
Your program will reads input from a text file using standard input redirection. The data will be in the following format:
3
1 40 10.67
2 21 15.71
3 100 1.52
The first integer will be the the number of employee’s data you have in standard input. Note that each subsequent row of the data represents an employee’s id, hours worked, and hourly rate of pay in this order. You can assume that all data will be correct and complete.
Your program will consist of three functions.
main()
The main function will read the first integer from standard input and create an array of employee structs of the proper size. You will also create two variables to remember the highest and lowest paid employees.
After calling the other functions below, main should print the result:
Highest Paid: [id]
$[total_salary]
Lowest Paid: [id]
$[total_salary]
The [id]
and [total_salary]
should be replaced with the correct data from the array of employee structs. So using the sample data above, the output should be:
Highest Paid: 1 $426.80
Lowest Paid: 3 $125.00
create_employees()
This function will populate your array of employee structs with the data from the text file
find_high_low()
This function will loop over the array of employees and update each struct with the total salary they will earn for their hours worked. You may assume that there is no overtime. While calculating this data, keep track of the employees with the highest and lowest total salary you have calculated.