I need help with a couple things.

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.

This is the first code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  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;
    while (i < phrase.size() ) {
        if (islower(phrase[i])) {
           if (phrase[i] < 'd') {
                cout << char(phrase[i]+otherShiftAmount);
           } else {
                cout << char(phrase[i]+shiftAmount);
           }
        } else {
            cout << phrase[i];
        }
        i++;
    }

    cout << endl;

    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string input;
  int count = 0, length;

  cout << "Enter your phrase: \n";
  getline(cin, input);

  length = (int)input.length();

  for (count = 0; count < length; count++)
    {
      if (isalpha(input[count]))
        {
          input[count] = tolower(input[count]);
          for (int i = 0; i < 13; i++)
            {
              if (input[count] == 'z')
                input[count] = 'a';
              else
                input[count]++;
            }
        }
    }

  cout << "Results: \n" << input << endl;

}
#1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}


#2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace 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;

}
Topic archived. No new replies allowed.