We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This C++ program takes an integer e and a string input, then applies a simple encryption algorithm to each digit in the input string by adding e to it and taking the result modulo 10. Here's the program rewritten with comments to explain each part:
include
using namespace std;
int main() {
int e; // Declare an integer variable e to store the encryption key
string input; // Declare a string variable input to store the input string
// Prompt the user to enter the input string followed by the encryption key
cout << "Enter the input string: ";
cin >> input;
cout << "Enter the encryption key: ";
cin >> e;
// Loop through each character of the input string
for (int i = 0; i < input.length(); ++i) {
// Encrypt the current digit by adding e and taking modulo 10
// Note: The expression 'input[i] - '0'' converts the character digit to its integer equivalent
// Then add the encryption key e and take modulo 10 to ensure the result is a single digit
// Finally, convert the result back to string and output it
cout << to_string((input[i] - '0' + e) % 10);
}
// Output a newline character for better formatting
cout << endl;
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Security Key Spaces
You are viewing a single comment's thread. Return to all comments →
This C++ program takes an integer e and a string input, then applies a simple encryption algorithm to each digit in the input string by adding e to it and taking the result modulo 10. Here's the program rewritten with comments to explain each part:
include
using namespace std;
int main() { int e; // Declare an integer variable e to store the encryption key string input; // Declare a string variable input to store the input string
}