-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from kingrishabdugar/master
#27 Method 2 in C++
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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. |