-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStingCompare_Recursive.cpp
43 lines (39 loc) · 1 KB
/
StingCompare_Recursive.cpp
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
/*
Task No 1:
Implement a recursive global function stringCompare which compares two character strings
recursively and:
1. returns 0 if the two strings are equal.
2. If the character of the first string at the index, where the first mismatch occurred, is
greater in ASCII value; then it returns 1
3. else it returns -1.
*/
#include <iostream>
using namespace std;
int stringCompare(const char* string1, const char* string2)
{
if (*string1 == *string2)
return 0;
if (*string1 > *string2)
return 1;
else
return -1;
}
int main() {
const char* str1 = "hello";
const char* str2 = "world";
int result = stringCompare(str1, str2);
if (result == 0)
{
cout << "The strings are equal.\n";
}
else if (result == 1)
{
cout << "String 1 is greater than String 2.\n";
}
else if (result == -1)
{
cout << "String 1 is smaller than String 2.\n";
}
system("pause>nul");
return 0;
}