0

I have tried a lot of solutions to print out the cypher text, but it is still not working. Now finally I have managed to successfully print the first 5 letters with the keyword bacon. However, the rest is still not correct. The loop with the keyword is not continued, what am I doing wrong? Do I miss a variable? Or....? Below a part of my code, please let me know if more is needed:

// get input from user
string sentence = GetString();


// check all characters
int keylength = strlen(sentence); 

for (int i = 0, k = 0; i < keylength; i++)
{
        // if it is a letter, check for the following
        if (isalpha(sentence[i]))
            {
                // if upper case for message and key
                if (isupper(sentence[i]) && isupper(key[k]))
                {
                    printf //code to print letter;
                }
                // if lower case for message and key
                else if (islower(sentence[i]) && islower(key[k]))
                {
                    printf //code to print letter;
                }
                // if upper case for message and lower case for key
                else if (isupper(sentence[i]) && islower(key[k]))
                {
                    printf //code to print letter
                }
                // if lower case for message and upper case for key
                else if (islower(sentence[i]) && isupper(key[k]))
                {
                    printf //code to print letter
                } 
                k++;
            }
     // for other values like numbers and special signs
     else
     {
          printf("%c", sentence[i]);
     }

So if I type the sentence: meet me at the park I get the following: Meet me at the park at eleven am Negh z xw zz ini bokwr se

As you can see, the first letters are ok, but after that it makes no sense. What do I not see? How to loop the keyword again to repeat the procedure to cypher the rest of the sentence?

1 Answer 1

1

You've got the right idea to use two different index variables for sentence and key, but you've forgotten a critical step. In your example, you had a key of 5 letters, so key[] is an array from 0 to 4.

Your code does nothing to reset k back to 0 when k gets to the end of the string. What happens when k is incremented to 5 and beyond? It will use whatever garbage data is located in those memory locations. Fix this and your problem should be solved.

Also, your variable keylength isn't actually the key length. It is the length of sentence, the plain text to be encoded. You need both string lengths for this program.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

2
  • Thank you! I saw indeed the mistake I made with key length instead of sentence. I now place k outside the loop and used the modulus to calculate the new k so that the word bacon will be repeated. Thank you for the tip!
    – Bossie
    Commented Sep 23, 2015 at 11:08
  • One other small questions: the program is now working but with the check I got the error: :( vigenere.c compiles \ expected an exit code of 0, not standard error of "clang: error: unable to execute command..." I've managed to solve this by deleting the enter between printf("\n") and return0;. Is there a reason why there can't be an extra enter between this printf and the return0 function?
    – Bossie
    Commented Sep 23, 2015 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .