Use C ++ to illustrate how to put words in a sentence upside down

Source: Internet
Author: User

In C ++, words in a sentence are put upside down (between words, words themselves are not put upside down), without punctuation. For example, if "I come from tianjin." is put into an inversion, it becomes "tianjin. from come I ".
C common library functions include:
Copy codeThe Code is 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, if the main string is "123456789" and the substring is "234", "23456789" is returned ".
Char * strcpy (char * DestStr, const char * SrcStr) Copy string Function
Int strcmp (const char * str1, const char * str2) compares two strings
Char * strcat (char * destStr, const char * srcStr) connection string

There are no library functions that are suitable for the question, so you can find a way to put them upside down without library functions. The following are your own implementations. I hope you can correct them !!!Copy codeThe Code is 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;
// First reverse all the strings to. nijnaiT morf emoc I
While (j> I)
{
Temp = str [I];
Str [I] = str [j];
Str [j] = temp;
J --;
I ++;
}
Cout <"string =" <str <endl;
// Perform partial word inversion. When a space is used, the end of a word is determined.
I = 0;
While (str [I])
{
If (str [I]! = '')
{
Begin = I;
While (str [I] & str [I]! = '')
{
End = I;
I ++;
}
If (str [I] = '\ 0') // end character of the 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;
}

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.