You're exactly right. For the keyword BACON, this code will process exactly 5 characters (the number of characters in BACON) and exit the loop. Here's why. Look at the control statement for the loop:
for(int i = 0,j = 0;j < key_length && i < p_text_length; i++)
There are two controls in place here. One is the length of the text to translate, the other is the length of the key. Whichever one gets hit first will terminate the loop.
Once that is fixed, there's still another issue. There's no mechanism here to wrap back around to the beginning of the key.
You're on the right track. You know that the length of the string to be encoded is a limit. You also realized that the length of the key is important. The problem here is that the code is written to terminate when the end of the key is reached. Instead, the code needs to go back to the beginning of the key and reuse it, making the key look something like "BACONBACONBACONBAC..."
You need a couple of design changes. First, the key length cannot be a control in the for loop. If the key is shorter than the string to be encoded, it will never encode the whole string. (Initializing j in the control statement is OK though.) Making this change will stop the encoding from terminating early. The statement becomes this:
for(int i = 0,j = 0; i < p_text_length; i++)
Next, you need to figure out how to go back to the beginning of the key after the last character has been used. Currently, you have the statement(s)
j++ to step through the key. For
BACON, with a length of 5, what happens when j increments to 5? Remember, this is 0 indexed, so 0 through 4 are the valid numbers.
It would be easy to tell you the fix, but more important that you figure it out yourself. For the specific case of BACON, what operator can you use to wrap around back to 0, when j increments beyond 4? In the general case, what is that operator and what is the value or limit to be used? You've actually used the operator already.
Here's a hint.
j++ ; becomes something like this:
j = ++j <some operator> limit_value;
Note: ++j works here, so does (j+1), but j++ doesn't. I'll let you figure out why.
You should be able to figure it out from here, but if you can't, leave a comment and we can help get you the rest of the way. ;-) This should get you going though.
If this answers your question, please accept this answer thus marking this question as answered. Let's keep up on forum housekeeping. ;-)