Skip to content

Commit f9fb569

Browse files
Create C++ Program to Compare Paths of Two Files.cpp
dorakit#27 method 2 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.
1 parent 0ada348 commit f9fb569

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Using iteration(for & while loop) :
2+
3+
To store paths use string as data type
4+
Use for or while loop and compare each character of them one by one.
5+
6+
Syntax:
7+
8+
while(path1[i] != '\0' || path2[i] != '\0'){
9+
//compare the character
10+
//increment value of i
11+
}
12+
13+
OR
14+
15+
for(int i = 0; path1[i] != '\0' || path2[i] != '\0'; i++){
16+
//compare the character
17+
}
18+
19+
Below is the implementation of the above approach:
20+
21+
// C++ Program to Compare Paths of Two Files
22+
// using for loop
23+
24+
#include <iostream>
25+
using namespace std;
26+
27+
// function to compare two paths
28+
void pathCompare(string p1, string p2)
29+
{
30+
// for loop to compare the paths
31+
for (int i = 0; p1[i] != '\0' || p2[i] != '\0'; i++) {
32+
// compare the character
33+
if (p1[i] != p2[i]) {
34+
cout << p1 << " is not equal to " << p2 << endl;
35+
return;
36+
}
37+
}
38+
cout << p1 << " is equal to " << p2 << endl;
39+
}
40+
41+
// Driver code
42+
int main()
43+
{
44+
string p1 = "/a/b/c";
45+
string p2 = "/a/b/";
46+
string p3 = "/a/b";
47+
string p4 = "/a/b";
48+
string p5 = "/a/b";
49+
string p6 = "/a/b.";
50+
pathCompare(p1, p2); // function call
51+
pathCompare(p3, p4); // function call
52+
pathCompare(p5, p6); // function call
53+
return 0;
54+
}
55+
56+
// This code is contributed by Susobhan Akhuli
57+
Output
58+
59+
/a/b/c is not equal to /a/b/
60+
/a/b is equal to /a/b
61+
/a/b is not equal to /a/b.

0 commit comments

Comments
 (0)