-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.1_reverse_string.cpp
More file actions
85 lines (75 loc) · 1.43 KB
/
Copy path1.1_reverse_string.cpp
File metadata and controls
85 lines (75 loc) · 1.43 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
using namespace std;
string reverseString(string actual, int len, int index)
{
if(len == index) { return ""; }
string temp = reverseString(actual,len,index+1);
return temp + actual[index];
}
void swap(char& a, char &b)
{
char temp = a;
a = b;
b = temp;
}
void ReverseString(string& actual)
{
int n = actual.size();
for(int i = 0; i < n/2; i++)
{
swap(actual[i], actual[n - i - 1]);
}
}
bool compareHelper(string& str1, string& str2, int len, int index)
{
if(len == index)
{
return true;
}
else
{
if(str1[index] == str2[index])
{
bool temp = compareHelper(str1, str2, len, index + 1);
cout << "Entering here" << endl;
return temp;
}
else
{
return false;
}
}
}
bool stringComparison(string& str1, string& str2)
{
int len1 = str1.length();
int len2 = str2.length();
if(len1 != len2)
{
cout << "Strings are not equal" << endl;
return false;
}
else
{
bool result = compareHelper(str1, str2, len1,0);
if(result)
{
cout << "Strings are equal" << endl;
}
else
{
cout << "strings are not equal" << endl;
}
return result;
}
}
int main()
{
string str = "Hello";
cout << "Actual array:" << str << endl;
cout << "Reverse Array:" << reverseString(str,5,0) << endl;
string strrev = "Hello";
stringComparison(str,strrev);
return 0;
}