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