Final Exam Practice Problems

Write a function that takes an array and a size as parameters and returns a pointer to a new dynamically allocated array which has the same size but has the original elements reversed.


Write a program which compares two strings given on the command line and prints the size of the largest prefix that the two strings have in common. For example, given "this" and "that", the program should print 2 because the first two characters are the same.

Place the code that compares the two strings in a function that takes the two strings as parameters and returns the size of the common prefix.


Write a program to draw an X out of asterisks, with a size specified by the user. Place the code that draws the X in a function which takes the size as a parameter.

*   *
 * *
  *
 * *
*   *

Add an option to the program so that it writes the X to a file instead of printing it if given a filename on the command line.


Write a program that finds the length of a string using a recursive function. Do not use strlen().


Write a program that counts the number of words in a file.


Write a program to find the sum of the digits of a positive integer. Try this with both a loop and with recursion.

Hint: You can get the rightmost digit of a positive integer n using n % 10, and you can remove the rightmost digit of a positive integer using n / 10.