I have what I think is a fairly simple question. I am relatively new to C++, and I know the basics of loops and of the language in general. However, I am a bit stuck on something, and I was wondering if someone might know of a solution.
I want to loop through a string, character by character. This is simple:
However, after looping through it once, I want to start looping through it again, but within the same "for" statement. I'm not sure what you would call it, I was thinking "recursive" or "repetitive" loop, but I'm not sure.
Hmm... Ok, I'll show you what I'm trying to do.
I'm trying to make a simple, console based XOR encryptor for strings ok text, input by the end user.
Therefore, I have a string "key" and a string "input".
I want to XOR each letter in "input" by the corresponding letter in "key". However, obviously, key will run out before the message does in most cases, and therefore I want to revert back to the start of "key", but continue from where i left off in "input".
char key[] = "myKey";
char toEncode = "blah some other string etc";
int offset = 0;
for (int i = 0; i < strlen(toEncode); ++i) {
if ( (i - offset+1) > strlen(key) ) // may not need +1. Test it *shrug*
offset += strlen(key) ;
// i is index into toEncode
// i-Offset is index into key
}
int offset = 0;
for (i = 0; i < inputLength; i++){
// if (i-offset etc..
char t = textToCrypt[i];
t = (char)(t ^ key[i - offset]);
textToCrypt[i] = t;
}