Caeser Cipher Program. Help wanted.
Jul 7, 2015 at 2:29pm UTC
So I have basically learnt this for fun on youtube, and for the most part I can understand what the guy had done, but there are some things I do not quite get.
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
int count = 0;
int l;
cout << "Enter a sentence: \n" << endl;
getline(cin, input);
l = (int )input.length();
for (count = 0; count < l; 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]++;
}
}
}
system("Color A" );
cout << endl;
cout << "Encryption: " << input << endl;
cout << endl;
system("PAUSE" );
return 0;
}
PROBLEM:
Line 18 to 29.
I don't quite understand what '(input[count])' does on line 18. In fact, I'm not sure whats happening with anything that has the square brackets '[]'
Please help me to understand with the square brackets imply and what they do? I really need to understand this. Sounds very interesting.
Could you also explain whats happening on line 14 as well? Much appreciated.
Last edited on Jul 7, 2015 at 2:35pm UTC
Jul 7, 2015 at 3:41pm UTC
the square brackets imply and what they do?
Square brackets are used to access array element:
input[0]
is first element,
input[1]
is next and so on.
input[i]
is elements with index
i .
whats happening on line 14
i
is assigned length of string. THere is cast here
(int )
to suppress warnings (and it is here because whoever wrote this code choose variable types poorly)
Jul 7, 2015 at 4:48pm UTC
So string count is acted like an array? And every character is indexed?
Last edited on Jul 7, 2015 at 4:48pm UTC
Jul 7, 2015 at 4:59pm UTC
String is array of characters by its definition.
Jul 8, 2015 at 4:44am UTC
http://www.cplusplus.com/doc/tutorial/arrays/
Topic archived. No new replies allowed.