-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy path07 Reverse the string.cpp
More file actions
50 lines (45 loc) · 983 Bytes
/
07 Reverse the string.cpp
File metadata and controls
50 lines (45 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
using namespace std;
void Reverse(char s[], int no)
{
char* B; // string in heap
B = new char[no];
int i, j;
for (i = 0; s[i] != '\0'; i++) // taking i upto last
{
}
i = i - 1; // setting i one last of string
for (j = 0; i >= 0; i--, j++) // incrementing j and decrementing i upto i is greater then 0
B[j] = s[i];
B[j] = '\0';
cout << "The reverse of string is " << B;
}
void Reverse1(char s[])
{
int i, temp, j;
for (j = 0; s[j] != '\0'; j++) // taking j to last of the string
{
}
j = j - 1; // setting j one before the last string
for (i = 0; i < j; i++, j--)
{
temp = s[i]; // swapping 1st char with last everytime
s[i] = s[j];
s[j] = temp;
}
cout << s;
}
int main()
{
char* s;
int l;
cout << "Enter the Size of the Array " << endl;
cin >> l;
s = new char[l];
cout << "Enter the String " << endl;
cin >> s;
Reverse(s, l);
cout<<endl;
Reverse1(s);
return 0;
}