C++ Program to Compare Paths of Two Files
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
Approach:
- 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++
// C++ Program to Compare Paths of Two Files#include <iostream>using namespace std;// function to compare two pathsvoid 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 codeint 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.


