File tree 1 file changed +42
-11
lines changed
1 file changed +42
-11
lines changed Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
2
using namespace std ;
3
- int main ( )
3
+ void Reverse ( char s[], int no )
4
4
{
5
- char A[]= " python " ;
6
- char B[ 7 ];
7
- int i;
8
- for (i= 0 ;A [i]!= ' \0 ' ;i++)
5
+ char * B; // string in heap
6
+ B = new char [no ];
7
+ int i, j ;
8
+ for (i = 0 ; s [i] != ' \0 ' ; i++) // taking i upto last
9
9
{
10
-
10
+
11
11
}
12
- i=i-1 ;
13
- for (int j=0 ;i>=0 ;i--,j++)
12
+ i = i - 1 ; // setting i one last of string
13
+ for (j = 0 ; i >= 0 ; i--, j++) // incrementing j and decrementing i upto i is greater then 0
14
+ B[j] = s[i];
15
+ B[j] = ' \0 ' ;
16
+ cout << " The reverse of string is " << B;
17
+ }
18
+ void Reverse1 (char s[])
19
+ {
20
+ int i, temp, j;
21
+ for (j = 0 ; s[j] != ' \0 ' ; j++) // taking j to last of the string
22
+ {
23
+ }
24
+ j = j - 1 ; // setting j one before the last string
25
+ for (i = 0 ; i < j; i++, j--)
14
26
{
15
- B[j]=A[i];
27
+ temp = s[i]; // swapping 1st char with last everytime
28
+ s[i] = s[j];
29
+ s[j] = temp;
16
30
}
17
- B[i]=' \0 ' ;
18
- cout<<" Reverse of the string is " <<endl<<B;
31
+ cout << s;
32
+ }
33
+
34
+ int main ()
35
+ {
36
+ char * s;
37
+ int l;
38
+ cout << " Enter the Size of the Array " << endl;
39
+ cin >> l;
40
+ s = new char [l];
41
+ cout << " Enter the String " << endl;
42
+ cin >> s;
43
+
44
+ Reverse (s, l);
45
+ cout<<endl;
46
+ Reverse1 (s);
47
+
48
+ return 0 ;
49
+
19
50
}
You can’t perform that action at this time.
0 commit comments