Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File handling #2

Open
Ishratbto opened this issue Jan 8, 2023 · 0 comments
Open

File handling #2

Ishratbto opened this issue Jan 8, 2023 · 0 comments

Comments

@Ishratbto
Copy link
Owner

#include
#include
#include
#include

int main() {
std::string file1, file2;
std::cout << "Enter the name of the first file: ";
std::cin >> file1;
std::cout << "Enter the name of the second file: ";
std::cin >> file2;

std::ifstream in1(file1);
std::ifstream in2(file2);
if (!in1 || !in2) {
std::cout << "Error opening one of the files.\n";
return 1;
}

int word_count1 = 0, word_count2 = 0;
std::string word1, word2;
while (in1 >> word1 && in2 >> word2) {
++word_count1;
++word_count2;
// Ignore case when comparing the words
std::transform(word1.begin(), word1.end(), word1.begin(), ::tolower);
std::transform(word2.begin(), word2.end(), word2.begin(), ::tolower);
if (word1 != word2) {
break;
}
}

std::cout << "Number of words in first file: " << word_count1 << '\n';
std::cout << "Number of words in second file: " << word_count2 << '\n';
if (word_count1 == word_count2) {
std::cout << "Files are 100% similar.\n";
} else {
std::cout << "Files are not similar.\n";
}

return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant