C++ Program to Compare Paths of Two Files
Last Updated :
15 Dec, 2022
Improve
As we are given two paths of two files, we have to compare these two paths and check whether they are equal or greater or smaller using a C++ program.
Input:
path1 = "/a/b/c" , path2 = "/a/b/"
Output:
path1 is greater than path2
Approaches:
Using built-in compare function :
- To store paths use string as data type
- Use pathname1. Compare( pathname2 ), to compare two paths , this will return three values greater that 0 , less than 0 or equal to 0
Example:
// C++ Program to Compare Paths of Two Files
#include <iostream>
using namespace std;
// function to compare two paths
void pathCompare(string p1, string p2)
{
// stores compared value 0 or >0 or <0
const int res = p1.compare(p2);
if (res > 0)
cout << p1 << " is greater than " << p2;
else if (res == 0)
cout << p1 << " is equal to " << p2;
else
cout << p1 << " is less than " << p2;
cout << "\n";
}
// 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;
}
Output
/a/b/c is greater than /a/b/ /a/b is equal to /a/b /a/b is less than /a/b.
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
// C++ Program to Compare Paths of Two Files
// using while loop
#include <iostream>
using namespace std;
// function to compare two paths
void pathCompare(string p1, string p2)
{
int i = 0;
// while loop to compare the paths
while (p1[i] != '\0' || p2[i] != '\0') {
// compare the character
if (p1[i] != p2[i]) {
cout << p1 << " is not equal to " << p2 << endl;
return;
}
i++;
}
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.
Using Comparison operators:
- To store paths use string as data type
- Use comparison operators (<, >, ==) to compare two paths.
syntax:
if(path1 > path2) // path1 is greater else if(path1 < path2) // path2 is greater else // both paths are same
Example:
// C++ Program to Compare Paths of Two Files
// using if-else condition
#include <iostream>
using namespace std;
// function to compare two paths
void pathCompare(string p1, string p2)
{
// Comparing using if-else
if (p1 > p2)
cout << p1 << " is greater than " << p2 << endl;
else if (p1 < p2)
cout << p1 << " is less than " << p2 << endl;
else
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 greater than /a/b/ /a/b is equal to /a/b /a/b is less than /a/b.
Time Complexity: O(1)
Auxiliary Space: O(1)