String reverse, word forward, and string reverse

Source: Internet
Author: User

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.