0

The for loop is not running and I cant figure out why. Everything looks good to me. I need some fresh eyes. Thanks guys!
#define _XOPEN_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <cs50.h>
#include <crypt.h>
#include <stdlib.h>

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

if (argc == 2)
{
    string key = (argv[1]);

    int letter;
    char range[60] =    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string plainText = get_string("plaintext: ");

    int keylength = strlen(argv[1]);

    printf("ciphertext: ");

    int j = 0;

    printf("%lu", strlen(plainText));
    for (int i = 0; i < strlen(plainText - 1); i++)
    {
        printf("IM HEEERRREEEEE");
        if(j == keylength - 1)
            {
                j = 0;
            }

            letter = plainText[i];
            printf("char %d - index %d",letter,i);
            if (letter > 96 && letter < 123)//if lowercase
            {
                if ((letter + key[j]) > 121)
                {
                    printf("%c", range[(((letter - 'a') + (key[j] - 'a')) % 26)]);
                    printf("%d, %d", letter, key[j]);
                    j++;
                }

                else
                {
                    printf("%c", range[((letter - 'a') + (key[j] - 'a'))]);
                    j++;

                }
            }

            else if (letter > 64 && letter < 91)//if uppercase
            {
                if ((letter + key[j]) > 89)
                {
                    printf("%c", range[((((letter - 'A') + (key[j] - 'A')) % 26) + 26)]);
                    j++;

                }

                else
                {
                    printf("%c", range[(((letter - 'A')+ (key[j] - 'A') - 'A') + 26)]);
                    j++;

                }
            }

            else // if special character
            {
                printf("%c", letter);
                j++;

            }
    }


printf("\n");
}

else
{
    printf("Please input the key\n");
    return 1;
}

return 0;

}

  • so just to be clear the "IM HEERREE!!!" will not run, or anything below it. – Turner Allen Aug 27 '18 at 22:55
1

Is this strlen(plainText - 1) really what you want? You want one less than the length of plainText. This is subracting 1 from plainText and calling strlen on the result. Methinks it always returns 0, thus the loop doesn't run.

| improve this answer | |
  • THANK YOU that's exactly it. I had it working before but mustve deleted a stupid parenthesis haha thank you very much! – Turner Allen Aug 27 '18 at 23:14

You must log in to answer this question.

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