1

I have a problem printing the word "world". My code is the following (EDIT):

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

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

    if (argc != 2)
    {
        printf ("Ingresar una clave \n");

        return 1;
    }else{
        for(int i = 0; i < strlen(argv[1]); i++)
        {
            if(!isalpha(argv[1][i]))
            {
                printf("Clave incorrecta\n");
                return 1;
            }

        }

    }


    string p = GetString();
    string k = argv[1];
    int n = strlen(k);
    //printf("%i", n);

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


        if(!isalpha(p[i]))
        {
            printf("%c", p[i]);
        }
        else if (isupper(p[i]))
        {
                if (isupper(k[j]))
            {


                p[i] = ((p[i] - 65) + (k[j%n]-65))% 26 + 65;
                j++;
                printf("%c",p[i]);

            }else{
                p[i] = ((p[i] - 65) + (k[j%n]-97))% 26 + 65;
                j++;
                printf("%c",p[i]);

            }

        }
        else if(islower(p[i]))
        {
            if (isupper(k[j]))
            {


                p[i] = ((p[i] - 97) + (k[j%n]-65))% 26 + 97;
                j++;
                printf("%c",p[i]);

            }else{

                p[i] = ((p[i] - 97) + (k[j%n]-97))% 26 + 97;


                int testn = n;

                printf("%i",testn);
                printf("%c",p[i]);
                printf("\n");
                j++;
            }
        }
    }

    printf("\n");

    return 0;
}

And the result I get is the following:

~/workspace/pset2/ $ ./vigenere baz
world
3x
3o
3q
3m
j

I don't know where the n (my key lenght) is going and "world" should be "xoqmd" instead of "xoqmj".

2
  • Can I see the original code without edits? I don't understand what happens on the ellipsis else. Oct 20 '16 at 12:43
  • Ok, I've added it Oct 20 '16 at 18:55
1

The line that should calculate that letter, should be 72.

0 1 2 3 4 w o r l d b a z b a 0 1 2 0 1

Since 'd' from "world" and 'a' from "baz" are both lower case. I think that line's algorithm is good so far.

You are doing a good job in wrapping around j by doing j%n. You have an examples of this on lines 48, 53, 66, 72.

But like Yuri Laguardia said, on lines 44 and 62 you refer to a plain j. This plain j without the %n is outside of the range of "baz". On the fourth and fifth iterations you are accessing k[3] and k[4] instead of k[0] and k[1] as I think you intend.

Just refer to j%n instead of a plain j on lines 44 and 62 and that should fix it.

1

I'll just make a question to see if it makes a click come to your mind:

What happens on these lines below when the key length is only 3 (like "baz") but j becomes 4 inside the loop?
if (isupper(k[j]))
if (isupper(k[j]))

You must log in to answer this question.

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