/** * string-reverse.c - An implemenation of string-reverse. Designed as * part of a sample solution to assignment 7. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include "string-utils.h" // For string_reverse // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Swap elements at positions i and j. */ void char_swap (char *str, int i, int j) { char tmp = str[i]; str[i] = str[j]; str[j] = tmp; } // char_swap // +--------------------+--------------------------------------------- // | Exported Functions | // +--------------------+ char * string_reverse (char *str) { int i; // Index variable int len = strlen (str); // Length, saved for efficiency. // For each position from 0 to the middle for (i = 0; i < len/2; i++) { // Swap that element with the element an equivalent distance // from the end. char_swap (str, i, len - i - 1); } // for // And we're done return str; } // string_reverse