1

I'm still trying to figure out why my code isn't working, if anyone can offer any insight, it would be very appreciated!

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

int main(int argc, string arvg[])

{
//this make prints an error is cla is not 2
    if (argc != 2)
    {
        printf("No good, it doesn't equal 2 \n");
        return 1;
    }
//this converts integers to characters
    int key = atoi(arvg[1]);

//this makes sure it's a non-negative integer
    if (key < 0)
    {
        printf("No good, it's zero\n");
        return 1;
    }
    else
    {
        string plaintext = get_string("plaintext: ");
        printf("ciphertext: ");
        for (int i = 0, n = strlen(plaintext); n < i; i++)
        {
//check lowercase
             if (islower(plaintext[i]))
            {
                printf("%c", (((plaintext[i] - 97 + key) % 26) + 97));
            }
//check uppercase
            else if (isupper(plaintext[i]))
            {
                printf("%c", (((plaintext[i] - 65 + key) % 26) + 65));
            }
            else
            {
                printf("%c", plaintext [i]);
            }

        }

    }
            printf("\n");

            return 0;
}
2
    for (int i = 0, n = strlen(plaintext); n < i; i++)

When the loop starts, what is the value of n and the value of i? Is n LESS THAN I at the start? Will this loop ever run?

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

2
  • @Xathia if this solved your problem, remember to click the grey check mark to the left of this answer! This marks a question as solved. – Tim Aug 9 '18 at 1:14
  • It finally worked!!!! Thanks so much!! – Xathia Aug 9 '18 at 20:15

You must log in to answer this question.

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