Achieve reverse (const char * s1, char * s2)

topic:

After the contents of the string s1 to s2 inverted output, such as s1 = "12345678", then the output of s2 is "87654321", can be implemented to select a language.

 Library requires not use any function comprises including strlen, and can not be defined in addition to other variables s1, s2 when the C language. C using the following function prototype void reverse (const char * s1, char * s2);

to sum up:

If the string constants defined, the default type is const type, also said that if we pass directly pass into the character, the type is const, that is, it can not be modified! ! !

String constants char * p1 = "1234"; String variable = char p1 [] = "1234 ';

Note that the first address at the time of transmission parameters, the best pass an array of characters, rather than the whole string into the pass 

When flipped, and then create an array pointing to another s2, this re-evaluation, if the direct move s2, the first address of the original bad back

 

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 #include<map>
 5 #include<vector>
 6 #include<cmath>
 7 #include<list>
 8 #include<stdlib.h>
 9 #include<ostream>
10 #include<set>
11 #include<queue>
12 #include<stack>
13 #include<cstring>
14 #include<deque>
15 using namespace std;
16 
17 # define ll long long
18 # define ull unsigned long long
19 # define inf 0x3f3f3f3f
20 # define ll_inf (1ll<<60)
21 const int maxn = 2e2 + 100;
22 
23 void reverse( const char *s1,char *s2)
24 {
25     int len,j;
26 
27     for( len = 0; s1[len] != '\0'; len++);
28     int i ;
29     char *tmp = s2;
30     for( i =0 ; i < len ; i++)
31     {
32 
33         char u =s1[len -i -1];
34         cout << u << endl;
35         *tmp = u;
36         tmp++;
37     }
38     cout << s2 << endl;
39 }
40 
41 int main()
42 {
43     char p[] = "1234";
44     char t[] = "4567";
45     reverse(p,t);
46 
47     return 0;
48 }

 

Guess you like

Origin www.cnblogs.com/letlifestop/p/11511069.html