Skip to content

Commit 243be19

Browse files
authored
Update 07 Reverse the string.cpp
1 parent 1f63160 commit 243be19

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

String/07 Reverse the string.cpp

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
#include<iostream>
22
using namespace std;
3-
int main ()
3+
void Reverse(char s[], int no)
44
{
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
99
{
10-
10+
1111
}
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--)
1426
{
15-
B[j]=A[i];
27+
temp = s[i]; // swapping 1st char with last everytime
28+
s[i] = s[j];
29+
s[j] = temp;
1630
}
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+
1950
}

0 commit comments

Comments
 (0)