0

i'm stuck on vigenere for very longtime and i don't know whats wrong with my code, can you please check it out and tell me what's wrong with it?

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

int main(int argc,string argv[])
{

    int g = strlen(argv[1]);
    if(argc != 2)
    {
        printf("type the index element\n");
        return 1;
    }

    for(int i = 0; i < g; i++)
    {

         if(!isalpha(argv[1][i]))
         {
            printf("type letters from a to z only please :|\n");
            return 1;
         }
    }
    printf("type your text here please: ");
    string x = GetString();

    for(int i =0, n = strlen(x); i<n;i++)
    {
        int key1 = (argv[1][i]-65)%g;
        int key2 = (argv[1][i]-97)%g;
        char result;
        if (isalpha(x[i]))
        {
            if (isupper(x[i]))
            {
                int num = ((x[i]-65) + key1)%26;
                result = num + 65;
            }
            if (islower(x[i]))
            {
               int num = ((x[i]-97) + key2)%26;
               result = num + 97;
            }
            printf("%c",result);
       }  
        else
        {
            printf("%c",x[i]);
        }       
    }
    printf("\n");   
}

1 Answer 1

1

So, keep in mind that i'm only into pset3 so I might not know what i'm talking about. That said I can spot a few problems with your code after playing with it for a bit.

Before that you should probably tell people what is going wrong. You should also comment your code to make it easier for others to compare what it's supposed to do with what it's actually doing.

First, your program has problems when it is passed no keyword. That is if it runs as ./vigenere it will keep running because argv does have 2 arguments. Remember that every string has a null character. argv might appear to you as ./vigenere but it really looks like ./vigenere '\0'

second there are two problems with your code I think. The first is here

int key1 = (argv[1][i]-65)%g;
int key2 = (argv[1][i]-97)%g;

If you run ~cs50/pset2/vigenere a you will noticed the string returned is the same one passed in however if you use b as the keyword it changes. For example keyword a returns bacon as bacon but keyword b returns bacon as cdbpo.

If you run your code with both keywords a will return fine but b won't.

Another problem that is related to the above code is when your keyword is shorter than the string you want to encrypt. If you use 'b' as a keyword

int key1 = (argv[1][i]-65)%g;
int key2 = (argv[1][i]-97)%g;

will work fine but if your text is longer than the keyword your program will keep pulling things from memory starting with the null character, I think.

After you fix that make sure that sentences are encrypted correctly. I had a problem where my program tried to encrypt spaces and punctuation which caused any following letters to be encrypted incorrectly. I think you might have the same problem too.

2
  • thanx man! i fixed the problems that u listed but i still have a problem, my program is encrypting the spaces so what do suggest me to do?
    – hisham
    Commented Apr 11, 2015 at 12:46
  • So from the code above your program works when the character is upper or lowercase. However if it is neither then nothing will happen but the variable i will increment and thus the place in your key also changes because it is dependent on i as well. Answer is that you'll only want to change the place in your key when either of those 2 if statements happen.
    – Stalris
    Commented Apr 11, 2015 at 15:01

You must log in to answer this question.

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