C + + string inversion

Source: Internet
Author: User

Title, enter a string, invert the word according to the space,

Problem Description:

1, the word composition: No space characters form a word

2. The input string can contain leading and trailing spaces, but the inverted characters cannot be included.

3. There are multiple spaces between each word

Method: First extract each word, each reversal.

Code:

#include <string>

#include <vector>

String reverseString1 (string& str)
{
Std::string::size_type POS;
std::string result;
str + = ""; Extend strings for ease of operation, preventing no trailing spaces
int size = Str.size ();
string ret;
for (int i = 0; i < size; i++)
{
pos = Str.find (", i);
if (Pos < size && pos! = i)
{
std::string s = str.substr (i, pos-i);
Reverse (S.begin (), S.end ());
RET + = s + "";
i = pos;
}
}
Ret.erase (--ret.end ()); Remove the last extra space
return ret;
Test Result: Input:

std::string Test ("I have a little dog.");
Test = reverseString1 (test);

Output: Test = "I evah a elttil. God"

C + + string inversion

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.