0

I am currently working on vigenere in pset2 and I cant figure out how to loop the keyword back around until the entire text is encoded. These are the errors I am getting from check50:

:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ expected output, but not "xoqli, qyh bfrgi!\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
   \ expected output, but not "CaQFti\n"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
   \ expected output, but not "CAQFTI\n"
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword

I’ve tried using the help from other posts, but none of them work. Can anyone help? Thanks!

1

2 Answers 2

0

Let text1 and text2 be two strings such that text1.length is greater than text2.length. The pseudocode for this should like this.

size1 = text1.length
size2 = text2.length
for i: 0 -> size1 :
     process(text1[i], text2[i%size2])

do anything in place of process() function.

Let size2 = 4. How actually it works :

i        :   0   1   2   3   4   5   6   7   8   9   10 ...
i%size2  :   0   1   2   3   0   1   2   3   0   1   2  ...
3
  • That worked, thank you! However, now I am getting this error: :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmd, szz gflkp!\n" I know it needs to skip spaces and punctuation, but I can’t figure out how to do that. I want to use isalpha to spot the non-alphabetic characters, but then I don’t know how to get the keyword to stay on the next letter of the keyword while skipping over spaces and punctuation. Do you know how I can get that to work?
    – KJK
    Commented Jul 29, 2014 at 14:38
  • 1
    @KJK For one thing, you could use two different variables. One for the index of the currently processed character and one for the currently used character in the keyword. You can then increment both when necessary.
    – skreborn
    Commented Jul 29, 2014 at 14:53
  • Thank you so much! I've figured it out.
    – KJK
    Commented Jul 30, 2014 at 17:05
0

The best way to do this would be with the modulo % operator. The modulo operator returns divides two numbers but only returns the remainder. So 5 % 2 = 1, and 10 % 5 = 0. You should set the variable you're using to iterate through your keyword to modulo the length of the key word.

You must log in to answer this question.

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