i really didn't want to ask on here but i cant see the problem for this really strange result. im not 100% finished with this code, i was just checking my algorithm was working before completing it. i have multiple printf() statements to see what is going on inside.

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

 *encripts user text using vigenere's cipher and user's key word

 int main (int argc, string argv[])

    //check if has one arg and is alphabetical
     if (argc != 2)
         printf("Please give only one key word without numbers\n");
         return 1;

     //get text from user and key to new variable
     string text = GetString(), key = argv[1];
     int j = 0;
     int check = strlen(key);
     printf("key: %s\n", key);
     //encript text
     for (int i = 0, n = strlen(text); i < n; i++)
         if (check - 1 < j)
             j = 0;
         printf("j: %i\n", j);
         if (isalpha(text[i]))
            if (islower(text[i]))

                 printf("before: %c\n", text[i]);
                 // change to alphabetical numerical representation
                 printf("key before: %c\n key num B: %i\n", key[j], key[j]);
                 key[j] -= 'a';
                 text[i] -= 'a';
                 printf("key num A: %i\n", key[j]);
                 //encript letter and change back to ASCII numerical representation
                 text[i] = (text[i] + key[j]) % 26 + 'a';
                 printf("num value %i\n after: %c\n", text[i], text[i]);
     printf("%s\n", text);

here is what my terminal is displaying

key: abc
j: 0
before: a
key before: a
key num B: 97
key num A: 0
num value 97
after: a
j: 1
before: a
key before: b
key num B: 98
key num A: 1
num value 98
after: b
j: 2
before: a
key before: c
key num B: 99
key num A: 2
num value 99
after: c
j: 0
before: a 
key before: 
key num B: 0
key num A: -97
num value 78
after: N
j: 1
before: a
key before: 
key num B: 1
key num A: -96
num value 79
after: O
j: 2
before: a
key before: 
key num B: 2
key num A: -95
num value 80
after: P

as you can see, the second time around it is not recognizing the nth position of key[]


nevermind guys i found the problem, i never reverted my key back to ASCII.

You must log in to answer this question.

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