0

I got stucked with Vigenere.c problemset2. With this program the user must prompt a command line keyword that will be used to encrypt a plaintext that the user will later enter. In particular for every letter of the keyword there is a correspondent alphabetical letter from 0 to 25 that will be added to the letter of the plaintext in order to encrypt the text. I tried to write all the program but I am not sure if the last two functions that I have implemented are right and moreover, I am not sure if I have concatenated the two for loops in a correct way. Anyone can help me and say which errors are there in this program. In particular when I run the program there are no errors but after I have entered a plaintext the program ends without encrypting it. Thank you for your help. Here's the code:

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int vigenere_low( char c )
{
    int v = c - 'a';
    return v;
}

int vigenere_up( char c )
{
    int v = c - 'A';
    return v;
}

int keyword_low( char c )
{
    int k = c - 'a';
    return k;
}

int keyword_up( char c )
{
    int k = c - 'A';
    return k;
}

int main( int argc, string argv[] )
{
    string p;
    string keyword = argv[1];

    if ( argc != 2 )
    {
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }

    for ( int i = 0, n = strlen(keyword); i < n; i++ )
    {
        if ( !isalpha( keyword[i]) )
        {
            printf("Usage: ./vigenere keyword(alphabetical) \n");
            return 1;
        }
    }

    p = GetString();

    for ( int i = 0, n = strlen(p); i < n; i++)
    {
      for ( int j = 0, l = strlen(p); l < n; j++)
      {
            if ( strlen( keyword ) < strlen( p ) )
            {
                j = j % strlen( keyword );
            }

            if ( isalpha( p[i] ) )
               if ( islower( p[i] ) )
               {
                  printf("%c", vigenere_low( p[i] ) + keyword_low( keyword[j]) );
               }
               else
               {
                  printf("%c", vigenere_up( p[i] ) + keyword_up( keyword[j]) );

               }
            else
            printf("%c", p[i] );
      }
    }
    return 0; 
}
0

The code doesn't seem to be getting into the inner for loop. Look at the setup for it:

for ( int j = 0, l = strlen(p); l < n; j++)

The var l is never updated inside the loop, but more important, the test condition doesn't seem to be true when this line is executed. What is the purpose of l?

It looks like you're trying to control the index for the key string with a loop. Since the key chars are not guaranteed to be used with each character in the plaintext, this won't work. The index needs to be incremented only when a letter is encoded.

Once you solve this, there are more problems waiting. What will this code do if the key is uppercase and the plain text is lowercase or vice versa? Are your calculations actually producing letters, or unprintable characters? There may be more issues.

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

| improve this answer | |
  • Thank you. I understand your correction. I know there are different issues, I'm a beginner, it's already a not so easy problem for me. So, the for loop works well only for the index of the string prompted and not for the keyword isn't it? Where I should introduce the J index in the program? I have to declare it earlier and then increment it (j++) when a particular condition ( that is the character of the string that needs to be alphabetical) is satisfied? – Fabio Operti Jul 9 '16 at 10:58

You must log in to answer this question.

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