/* ----------------------------------------------------------------------- * * Exercise 13-3: A set of functions that plays with words * 1) reverses the whole string * 2) reverses each word in the string, preserving original * word order * 3) reverses the words in the string, preserving the order * of letters in words. * * Author: Suzi Anvin * ----------------------------------------------------------------------- */ #include #include /* swap_chars takes pointers to 2 places in a string. It swaps the poited at * characters, then moves the pointers inwards, and continues swapping until * all characters have been swapped, reversing the order of the parts of the * string between the two pointers */ void swap_chars (char *first_ptr, char *end_ptr) { char tmp; /* holds one character for a character swap */ while (first_ptr < end_ptr) { /* loop until the two pointers meet or cross */ tmp = *end_ptr; /* save the character at the end to tmp */ *end_ptr = *first_ptr; /* move the first character to the end */ *first_ptr = tmp; /* move the saved end chacater to the front */ ++first_ptr; /* move the pointers inward one step */ --end_ptr; } } /* Reverse the order of characters in the entire string */ void reverse_string (char *first_ptr, int size) { /* first_ptr points at the first char in the string */ /* size is the number of characters in the string */ char *end_ptr; /* points at the last char in the string */ end_ptr = first_ptr + size - 1; /* point end_ptr at last character */ swap_chars (first_ptr, end_ptr); /* reverse the order of characters */ } /* Reverse the order of characters within each word */ void reverse_word_chars (char *first_ptr) { /* first_ptr points at first character in a word */ char *end_ptr; /* points at last character in a word */ char *space_ptr; /* points at the space after the word to be swapped*/ space_ptr = first_ptr; while (*space_ptr != '\0') { /* choose new words until end of string */ while ( *space_ptr != ' ' && *space_ptr != '\0') { space_ptr++; /* advance space_ptr to end of next word (' ' or '\0') */ } end_ptr = space_ptr - 1; /* set end of word */ swap_chars (first_ptr, end_ptr); /*reverse the order of the characters */ ++space_ptr; /* move space_ptr so it no longer points to a space! */ first_ptr = space_ptr; /* set beginning of next word */ } } /* Reverse the order of words only*/ void reverse_word_order (char *string, int size) { reverse_string (string, size); reverse_word_chars (string); } int main () { char user_string[100]; /* line of text user wants to modify */ char option[5]; /* option entered by user */ while (1) { fputs("Please type in the text you wish to modify:\n", stdout); fgets(user_string, sizeof(user_string), stdin); user_string[strlen(user_string)-1] = '\0'; /* trim off '\n' */ fputs("Choose a text modification:\n" "1) Reverse the order of characters in the entire string\n" "2) Reverse the order of characters within each word\n" "3) Reverse the order of words only\n", stdout); while (1) { fgets(option, sizeof(option), stdin); switch (option[0]) { case '1': /* Reverse the order of characters in the entire string */ reverse_string(user_string, strlen(user_string)); break; case '2': /* Reverse the order of characters within each word */ reverse_word_chars(user_string); break; case '3': /* Reverse the order of words only */ reverse_word_order(user_string, strlen(user_string)); break; default: fputs("Error: Unknown text modification. Enter 1, 2, or 3", stdout); continue; /* go back to beginning of loop to re-enter option code*/ } printf("Your string:\n%s\n", user_string); break; } fputs("type Q to quit or [enter] to continue: ", stdout); fgets(option, sizeof(option), stdin); if (option[0] == 'Q' || option[0] == 'q') break; } return (0); }