0

The title says it all, I can't get my key to repeat itself correctly after one repetition. For example, if my key is 3 letters and GetString is 6, then the last3 letters of GetString will be incorrectly translated. Here's my code:

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("Enter your key after ./vigenere\n");
    return 1; 
}

string key = argv[1];

printf("Enter plaintext: ");
string p = GetString();

int i;

for (i = 0; i < strlen(p); i++)
{

        if (isspace(p[i]))
        {
            printf(" ");
        }
        if (ispunct(p[i]))
        {
            printf("%c", p[i]);
        }
        if (isalpha(p[i]))
        {

            int code = (p[i] + key[i]) % 26;

            if (isupper(p[i]))
            {



                if (code == 18)
                {
                    printf("%c", code + 47);
                }


                if (code > 18)
                {

                   if ((code + 39) < 91)
                   {
                        printf("%c", code + 65);
                   }
                   if ((code + 39) >= 91)
                   {
                        printf("%c",((code + 39) % 91) + 65);
                   }
                }


                if (code < 18)
                {
                    if ((code + 65) < 91)
                    {
                        printf("%c", code + 65);
                    }
                    if ((code + 65) >= 91)
                    {
                        printf("%c", ((code + 65) % 91) + 65);
                    }
                }
                }

            }

2 Answers 2

0

The key and the plaintext to be encoded need separate, independent index vars. The plain text index will be incremented for every char in the string. BUT, the index for the key will only be indexed when an actual alpha is encoded, thus consuming the next char in the key.

In another difference, the plaintext index will only be incremented from 0 to the length of the string, while the index for the key has to recycle back to 0 over and over, as needed, and as you have noticed.

There may be other issues, but this addresses your question.

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

0

First of all, I think your title has it the other way around, your text says key has 3 letters and string has 6. It's not working because you're not checking if string is longer than key. If it is, then your should repeat your key until you have the same number of letters as the string. The line

`for (i = 0; i < strlen(p); i++)` 

won't work with

 int code = (p[i] + key[i]) % 26;

if strlen(p)>strlen(key) for obvious reasons.

2
  • Sorry if this is a stupid question, but what obvious reasons exactly?
    – K. Roe
    Commented Oct 27, 2015 at 4:13
  • Let's say key is 'key' and string is 'string'. 's' will be encoded using 'k' as first letter in alphabet, 't' using 'e', and 'r' using 'y'. At this point you still need to encode 'ing' but you've run out of letters in your key. Your code should be able to loop back to the beginning of your key to reuse it again until every character in your string is encoded.
    – ronga
    Commented Oct 27, 2015 at 8:45

You must log in to answer this question.

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