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++
// 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++
// 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++
// 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++
// 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)
Similar Reads
C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad
1 min read
C++ Program to Create a File Problem Statement:Write a C++ program to create a file using file handling and check whether the file is created successfully or not. If a file is created successfully then it should print "File Created Successfully" otherwise should print some error message. Approach:Declare a stream class file and
2 min read
How to Compare Two Lists in C++ STL? In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Inpu
2 min read
C++ Program To Check if Two Matrices are Identical The below program checks if two square matrices of size 4*4 are identical or not. For any two matrices to be equal, the number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal. Recommended: Please solve it on "PRACTICE" first, before moving
2 min read
How to Compare Two Deques in C++? In C++ the Standard Template Library (STL) provides a container called deque (short for double-ended queue) that allows fast insertions and deletions at both ends of the deque. In this article, we will learn how to compare two deques in C++. Example: Input: deque1 = {10,20,30}; deque2 = {10,20,30};
2 min read