C++ Program to Compare Paths of Two Files
Last Updated :
15 Dec, 2022
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++
#include <iostream>
using namespace std;
void pathCompare(string p1, string p2)
{
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" ;
}
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);
pathCompare(p3, p4);
pathCompare(p5, p6);
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++
#include <iostream>
using namespace std;
void pathCompare(string p1, string p2)
{
for ( int i = 0; p1[i] != '\0' || p2[i] != '\0' ; i++) {
if (p1[i] != p2[i]) {
cout << p1 << " is not equal to " << p2 << endl;
return ;
}
}
cout << p1 << " is equal to " << p2 << endl;
}
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);
pathCompare(p3, p4);
pathCompare(p5, p6);
return 0;
}
|
C++
#include <iostream>
using namespace std;
void pathCompare(string p1, string p2)
{
int i = 0;
while (p1[i] != '\0' || p2[i] != '\0' ) {
if (p1[i] != p2[i]) {
cout << p1 << " is not equal to " << p2 << endl;
return ;
}
i++;
}
cout << p1 << " is equal to " << p2 << endl;
}
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);
pathCompare(p3, p4);
pathCompare(p5, p6);
return 0;
}
|
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++
#include <iostream>
using namespace std;
void pathCompare(string p1, string p2)
{
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;
}
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);
pathCompare(p3, p4);
pathCompare(p5, p6);
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.
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...