geeze...no line breaks? Anyway, you had a decent question, so I'm going to do this for you...
Code:
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
const int MAXCHAR = 80;
int k1, k2, k3;
int s1[MAXCHAR], s2[MAXCHAR], s3[MAXCHAR];
unsigned s1size, s2size, s3size;
bool generate = false;
ifstream infile ("wscipher.in");
if (!infile)
{
cerr << "Can't open wscipher.in" << endl;
exit (2);
}
ofstream outfile ("wscipher.out");
if (!outfile)
{
cerr << "Can't open wscipher.out" << endl;
exit (2);
}
while ((infile >> k1 >> k2 >> k3) && (k1 + k2 + k3 != 0))
{
unsigned i;
string instring;
int target;
infile.ignore (80, '\n');
infile >> instring; // ok, no spaces
if (generate)
reverse (instring.begin (), instring.end ());
string outstring (instring);
s1size = s2size = s3size = 0;
for (i = 0; i < instring.size (); i++)
{
if (instring[i] >= 'a' && instring[i] <= 'i')
s1[s1size++] = i;
else if (instring[i] >= 'j' && instring[i] <= 'r')
s2[s2size++] = i;
else
s3[s3size++] = i;
}
for (i = 0; i < s1size; i++)
{
target = s1[(i + k1) % s1size];
outstring[target] = instring[s1[i]];
}
//cerr << "outstring = " << outstring << endl;
for (i = 0; i < s2size; i++)
{
target = s2[(i + k2) % s2size];
outstring[target] = instring[s2[i]];
}
//cerr << "outstring = " << outstring << endl;
for (i = 0; i < s3size; i++)
{
target = s3[(i + k3) % s3size];
outstring[target] = instring[s3[i]];
}
//cerr << "outstring = " << outstring << endl;
if (generate)
{
outfile << k1 << " " << k2 << " " << k3 << endl;
reverse (outstring.begin (), outstring.end ());
}
//cerr << "outstring = " << outstring << endl;
outfile << outstring << endl;
}
return 0;
}
>>Why the bool ?
bool generate = false;
...
if (generate)
The bool is there for that if statement...if you read through what the if statement does, you can see what would happen if you changed the first line to
bool generate = true; in the code.
>>The series of code lines that are like these:
>>s1[s1size++] = i;
>>This is bit manipulation right?
No. If you look a few lines before that, you'll see
s1size = s2size = s3size = 0;
s1 is defined up at the top as int s1[MAXCHAR], meaning that it is an array of size MAXCHAR. s1size++ increments s1size. It's the same as s1size=s1size+1. So, s1[s1size++] is accessing the element in the array s1 after the one that was just accessed (in the last repetition of the loop).
As for your other question...I dunno, I've never done much with strings.
[edit] Somehow, the code got doublespaced when I copied it.