// This program takes in a string, and outputs that // string in reverse // Week 4 Lecture 8 #include #include // Define the size of the array = this is clearly going to cause some // waste - one of the downfalls of arrays #define MAX_LENGTH 124 int main(void) { // Declare our array to hold the string char word[MAX_LENGTH]; char reverse_word[MAX_LENGTH]; printf("Type in a word: "); // Read in the string using fgets() function fgets(word, MAX_LENGTH, stdin); int i = 0; while (word[i] != '\n') { i++; } word[i] = '\0'; //getchar(); // Do some magic here to calculate: // How do we start at the back of the string to print // out the characters of the string? Should we have another // array to hold the output? int j = strlen(word) - 1; for (i = 0; word[i] != '\0'; i++) { reverse_word[i] = word[j]; j--; } reverse_word[i] = '\0'; printf("The %s in reverse is %s\n", word, reverse_word); return 0; }