C Practice Exercises

Here are some practice exercises you can try.

Difficulty Levels

 *	easy
 ** 	medium
 ***	harder
 ****	Dude, ouch!

Structures

  1. * Write a structure definition called book, which encapsulates the following information: book title (a string), book author (a string), year of publication (integer), genre (e.g. mystery, fantasy, romance, etc) -- (a string), price (double).
     
  2. * Write a structure definition called time, which represents a clock time -- clock information should include hours, minutes, seconds, and AM/PM.
     
  3. * Write a function that takes in a time structure (from the exercise above) and prints it out in the format:
      hh:mm:ss (am|pm)
    
      Examples:  09:23:55 am  ,  12:03:01 pm
    
  4. ** Using the fraction structure from the notes pages, write a function called equals that takes in two fraction paramaters and returns 1 (for true) if the two fractions are equivalent in value, and returns 0 (for false) otherwise. Example calls:
      struct fraction f1 = {1,2}, f2 = {2,4}, f3 = {1,3};
    
      if (equals(f1, f2))			// this should come out true
        printf("f1 and f2 are equal\n");
    
      if (equals(f2, f3))			// this should come out false
        printf("f2 and f3 are equal\n");
    
  5. * Exercise 10.6 (page 425 in the textbook)

String and character exercises (Ch 8)

  1. ** Write a function called firstString that takes in 3 string parameters and returns the string (i.e. returns a pointer to the string) that comes first alphabetically. Example call:
      firstString("doghouse", "Doggerel", "dogfood")
            // this function call should return "dogfood"
    
     
  2. ** Write a function called strtrim that takes in a string and removes any leading and trailing white space from the string. The function prototype would be:
      void strtrim(char* str);
    
    A sample call:
      char buffer[40] = "    The quick brown fox     ";
    
      strtrim(buffer);
      printf("*%s*", buffer);	// this prints: 
    				// *The quick brown fox*
    
  3. ** Write a program that continually reads words input on the keyboard (stopping once the user types the newline character), and then prints out how many times the word "the" appears in the input.
     
  4. **** Write a function called replaceAll with the following prototype:
      void replaceAll(char* buffer, const char* oldstr, const char* newstr);
    
    The function should modify buffer so that all occurences of the string oldstr are replaced with newstr. Example:
      char buf[80] = "Concatenate the bobcat with the catapult";
      char str1[] = "cat";
      char str2[] = "bird";
    
      replaceAll(buf, str1, str2);
    
      puts(buf);	// prints:  "Conbirdenate the bobbird with the birdapult"
    

Arrays and pointers (Ch 6, 7)

  1. * Write a function that takes in an integer array, as well as its size, and returns a count of all the negative numbers in the array
     
  2. * Exercise 7.9 (page 303 of the textbook)
     
  3. ** Write a function that takes in as parameters an integer array and its size, and returns the sum of all the odd values in the array
     
  4. *** Exercise 6.15 (page 248 in the textbook)
     
  5. ** Exercise 6.19 (page 250 in the textbook)
     

Functions (ch 5)

  1. * Write a function called power that takes in two integeres, representing a base and an exponent, and returns the value of: baseexponent
      power(3,5)	// returns 3 to the 5th power...  which is 243
    
  2. ** Write a function that takes in 2 integers and returns their GCD (greatest common divisor). Example:
      GCD(100,60)	// returns 20
    
  3. ** Write a function that takes in 3 integer parameters and returns the LCM (least common multiple) of the 3 values. Example:
      LCM(3,4,10)	// returns 60, which is the smallest multiple of 3, 4, and 10.
    
  4. ** Write a function that takes in two variables of type double. Assume these will be positibe values. The function should consider these two values to be the legs of a right triangle, and the function should compute and return the hypotenuse