Skip to content
Related Articles

Related Articles

C++ Program to Compare Paths of Two Files

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 22 Sep, 2022
View Discussion
Improve Article
Save Article

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 :

  1. To store paths use string as data type 
  2. 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 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) :

  1. To store paths use string as data type
  2. 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++




// 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 compate 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++




// 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 compate 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 Comparision operators:

  1. To store paths use string as data type
  2. 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++




// 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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!