Exam 2 Practice Problems

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 that reads point coordinates from standard input, stores the coordinates in an array of structure instances representing the points, and then calculates and prints the total distance of all the line segments formed by following the points in order.

As you read in the coordinate values, store them in a dynamically allocated array which grows as needed. If an odd number of values is entered, indicate this with an error message and a non-zero exit code.

Creating a dynamically allocated array of structure instances works the same way as creating a dynamically allocated array of numeric values, you just need to multiply the desired number of elements by the size of the structure to calculate the number of bytes: capacity * sizeof(struct point)

Place the code that calculates the total distance in a function, and the code that calculates the distance between any two points in another function.

For example:

./distance
1 2.5
4.6 8.2
10 10
The distance is 12.433761

There are 3 points in that example. The distance is calculated by adding the distance from (1, 2.5) to (4.6, 8.2) and the distance from (4.6, 8.2) to (10, 10). The program should work with any number of points.


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 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 (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!

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.