The use of inverted function reverse

Source: Internet
Author: User

Inverted string function reverse: Used to invert the position of each character in the string s, such as the original string if the initial value is 123456, then through the reverse function can be inverted to 654321, the program is as follows:
#include <stdio.h>
#include <string.h>
void reverse (char s[])
{
int c,j,i;
For (I=0,j=strlen (s) -1;i<j;i++,j--)//complete inverted function, excluding string Terminator '/0 '
{
C=s[i];
S[I]=S[J];
S[j]=c;
}
}
Main ()//main function, used to test the function of the reverse function
{
Char s[]= "123456";
Reverse (s);//Call inversion function
printf ("Inverted string:%s/n", s);//Output inverted string
}

The results of the operation are as follows; 654321

function reverses the order of characters in the string. Syntax reverse (string)

Argument string: String that returns the value of the character order to reverse. When the function executes successfully, returns a string that reverses the character order, and returns an empty string ("") if an error occurs. Use the reverse () function to place the last character in a string to the first character position of another string, the second-to-penultimate character in the second character position of another string, and so on.

EXAMPLE:

Reverse Algorithm Example

#include <iostream>

#include <algorithm>

#include <vector>

using namespace Std;

int main () {

Vector<int> Myvector;

Vector<int>::iterator it;

Set some values:

for (int i=1; i<10; ++i) Myvector.push_back (i); 1 2 3 4 5 6 7 8 9

Reverse (Myvector.begin (), Myvector.end ()); 9 8 7 6 5 4 3 2 1

Print out content:

cout << "myvector contains:";

For (It=myvector.begin (); It!=myvector.end (); ++it)

cout << "" << *it;

cout << Endl;

return 0;

}

Output:

Myvector Contains:9 8 7 6 5 4 3 2 1

The use of inverted function reverse

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.