-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathC++ Program to Compare Paths of Two Files.cpp
61 lines (49 loc) · 1.32 KB
/
C++ Program to Compare Paths of Two Files.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Using iteration(for & while loop) :
To store paths use string as data type
Use for or while loop and compare each character of them one by one.
Syntax:
while(path1[i] != '\0' || path2[i] != '\0'){
//compare the character
//increment value of i
}
OR
for(int i = 0; path1[i] != '\0' || path2[i] != '\0'; i++){
//compare the character
}
Below is the implementation of the above approach:
// C++ Program to Compare Paths of Two Files
// using for loop
#include <iostream>
using namespace std;
// function to compare two paths
void pathCompare(string p1, string p2)
{
// for loop to compare the paths
for (int i = 0; p1[i] != '\0' || p2[i] != '\0'; i++) {
// compare the character
if (p1[i] != p2[i]) {
cout << p1 << " is not equal to " << p2 << endl;
return;
}
}
cout << p1 << " is equal to " << p2 << endl;
}
// Driver code
int main()
{
string p1 = "/a/b/c";
string p2 = "/a/b/";
string p3 = "/a/b";
string p4 = "/a/b";
string p5 = "/a/b";
string p6 = "/a/b.";
pathCompare(p1, p2); // function call
pathCompare(p3, p4); // function call
pathCompare(p5, p6); // function call
return 0;
}
// This code is contributed by Susobhan Akhuli
Output
/a/b/c is not equal to /a/b/
/a/b is equal to /a/b
/a/b is not equal to /a/b.