Repetitive Loop?

Hello,

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:
1
2
3
4
for(int i=0;i<strlen(input);i++)
{
output[i] = input[i];
}


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.

Hopefully, someone here can help me with this.

Regards,
Chris Matthews
Nested Loop.

1
2
3
4
5
6
7
char buf[] = "blah blah blah";

for (int i = 0; i < strlen(buf); ++i) {
  for (int j = 0; j < strlen(buf); ++j) {
   // do stuff
  }
}
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".
Last edited on
ahh ok.

1
2
3
4
5
6
7
8
9
10
11
12
13
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
 
}
Ok, thanks.

Sorry for my stupidity, but where would I put my code, for example, here is my current loop:

1
2
3
4
5
6
for (i = 0; i < inputLength; i++)
      {
              char t = textToCrypt[i];
              t = (char)(t ^ key[i]);
              textToCrypt[i] = t;
      }
1
2
3
4
5
6
7
8
9
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;
}
Thanks, it works brilliantly!! Now all I have to do is work out why it keeps randomly crashing...

Thanks for your help!
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>
using namespace std;


int main()
{

  int offset = 0;
  char textToCrypt[] = "some random text";
  char key[] = "myKey";

  int length = strlen(textToCrypt);

  cout << "Before: " << textToCrypt << endl;
  for (int i = 0; i < length; i++){
    if ( (i - offset) >= strlen(key) ) // may not need +1. Test it *shrug*
      offset += strlen(key);

      char t = textToCrypt[i];
      t = (char)(t ^ key[i - offset]);
      textToCrypt[i] = t;
   }

   cout << "Encrypted: " << textToCrypt << endl;
   offset = 0;
   for (int i = 0; i < length; i++){
     if ( (i - offset) >= strlen(key) )
       offset += strlen(key);

       char t = textToCrypt[i];
       t = (char)(t ^ key[i - offset]);
       textToCrypt[i] = t;
    }
    cout << "De-Crypted: " << textToCrypt << endl;
}


That works. Encrypted won't print out to console correctly because it contains null-printable values.
Thank you! That works perfectly. I think I may be using scanf incorrectly, causing some crashes, but I'm sure that can be solved easily enough!

Thanks! Solved!
You should avoid using scanf. It's the C way to do things. Get familiar with iostreams. Thats C++.
Topic archived. No new replies allowed.