String reverse, word forward, and string reverse
// String reverse Output Iput: Love you, No reason Output: reason No, you Love # include <iostream> # include <algorithm> # include <cassert> # include <cctype> using namespace std; void ReverseString (char * const str, const int len ); void ReverseAllWord (char * str); int main () {char str [] = "Love you, No reason"; ReverseAllWord (str); cout <str <endl; return 0;} void ReverseString (char * const str, const int len) {if (str = NULL | len <2) return; for (int I = 0; I <(len/2); I ++) {swap (str [I], str [len-i-1]);} void ReverseAllWord (char * str) {assert (str! = NULL); ReverseString (str, strlen (str); cout <str <endl; char * word_start = str; char * word_end = str; char * temp = str; while (* temp) {if (isalpha (* temp) {word_start = temp; word_end = temp; while (isalpha (* word_end) word_end ++; int word_len = word_end-word_start; ReverseString (word_start, word_len); temp ++ = (word_len-1) ;}temp ++ ;}}
First, this string is reversed, and then each word is restored (reverse again ).