In C + + implementation, the words in a sentence inversion method to explain the _c language

Source: Internet
Author: User
In the C + + language, the words in the word are inverted (the word inverted, the word itself is not inverted), punctuation is not caused. such as a sentence "I come from Tianjin." "and turned upside down to" Tianjin. From Come I ".
c Common library functions are:
Copy Code code as follows:

int strstr (const char* string,const char* substring) is used to return all characters after the position of the substring of the main string. For example, the main string is "123456789" and the substring is "234", then return "23456789".
char* strcpy (char* deststr,const char* srcstr) Copy string function
int strcmp (const char* str1,const char* str2) compare two strings
char* strcat (char* deststr,const char* srcstr) connection string

There is no library function that is too appropriate, so find ways to invert it without using a library function. Below is the realization of their own, deficiencies, but also look at the!!!
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
using namespace Std;
Char *strreverse (char * sourcestr)
{
Int j = 0, i = 0,begin,end;
char *str = SOURCESTR;
char temp;
j = strlen (str)-1;
cout << "string =" << str << Endl;
The string is first reversed into. nijnait Morf EmOC I
while (J > i)
{
temp = Str[i];
Str[i] = Str[j];
STR[J] = temp;
J--;
i + +;
}
cout << "string =" << str << Endl;
Then to the word part of the reversal, encountered spaces, then determine the end of a word
i = 0;
while (Str[i])
{
if (Str[i]!= ')
{
begin = i;
while (Str[i] && str[i]!= ')
{
end = i;
i++;
}
if (str[i] = = ' Terminator ')//String
{
i--;
}
}
while (End > Begin)
{
temp = Str[begin];
Str[begin] = Str[end];
Str[end] = temp;
End--;
Begin + +;
}
i + +;
}
cout << "string =" << str << Endl;
return str;
}
int _tmain (int argc, _tchar* argv[])
{
Char str[] = "I come from Tianjin."
StrReverse (str);
return 0;
}

Related Article

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.