I have been struggling on this for a while now. First I am confused as to what the section of code phrase[i])) does.
cout << char(phrase[i]
cout << char(phrase[i]
The second code does the same thing "input[count]" I am not sure what that syntax is.
Also for the second code I am confused on how to make a function out of that.
However I am not sure how I would even set one up for that code. Can anyone help? Thanks.
int main() {
string phrase;
char encodeOrDecode;
int i;
int shiftAmount = -3;
int otherShiftAmount = 23;
cout << "Enter a phrase to encode =>";
getline(cin, phrase);
cout << "Encode (E) or decode (D) =>";
cin >> encodeOrDecode;
encodeOrDecode = toupper(encodeOrDecode);
if(encodeOrDecode == 'D') {
shiftAmount = 3;
otherShiftAmount = -23;
}
cout << phrase << endl;
i = 0;
// Loops through all of the letters in the phrase
// Could be replaced with a for loop instead.
while(i < phrase.size()) {
// Checks to see if the letter in the phrase is a lowercase
if(islower(phrase[i])) {
// Checks to see if it's letters a - c
if(phrase[i] < 'd') {
// If it is, the letter has the value of 23 removed to it
// and that character is displayed on the screen
cout << char(phrase[i] + otherShiftAmount);
}
else {
// Otherwise 3 is added to the letter, typically resulting in another letter
// that value is then displayed as a character on the screen.
cout << char(phrase[i] + shiftAmount);
}
}
// If the letter in the phrase is anything other than a lowercase letter
else {
// Display it to the screen.
cout << phrase[i];
}
i++;
}
cout << endl;
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main() {
string input;
int count = 0, length;
cout << "Enter your phrase: \n";
getline(cin, input);
length = (int)input.length();
// Another way to write the for loop from #1
// iterates across the phrase you just inputted, starting at the first value
for(count = 0; count < length; count++) {
// If the current value can be represented as a letter
if(isalpha(input[count])) {
// make it lowercase
input[count] = tolower(input[count]);
// this basically just increases the letter by 13, also known as ROT13 encryption
for(int i = 0; i < 13; i++) {
// once the letter hits z, it goes back to a to prevent odd characters.
if(input[count] == 'z')
input[count] = 'a';
else
input[count]++;
}
}
}
cout << "Results: \n" << input << endl;
}