0

Having such a hard time getting the key to cycle back after first iteration, any hint would be great as i've been stuck on this for a long time. it does baconbbaconbbacon.

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

int main(int argc, string argv[])
{
    int i = 0;
    string s = argv[1];
    //recieve first argument
    if (argc != 2)
    {
        printf("Key not provided, try again.\n");
        return 1;
    }
    //check to see if argv[1] is alphabetical
    if (isalpha(s[i]))
    {
        for (int n = strlen(s); i < n; i++)
        {
            printf("Current int: %c converted to: %i\n", s[i], s[i]);
        }
        printf("Alphabet check passed...Proceeding...\n");
        printf("%s\n", s);
    }
    else
    {
        //non-alphabetical key entered
        printf("Key is not alphabetical\n");
        return 1;
    }
    //ask for plaintext
    printf("plaintext: ");
    string plain = get_string();
    //ciphertext
    printf("ciphertext: ");
    s = argv[1];
    i = 0;
    int conv = s[i];
    if (s != NULL)
    {
        for (int k = 0, x = strlen(plain), key = 0, n = strlen(s); k < x; k++, key++)
        {
            if (i == 0)
            {
                conv = s[i];
                i++;
            }
            else if (i < n)
            {
                conv = s[i];
                i++;
            }
            else if (i >= n)
            {
                i = (i - i);
                conv = s[i];
            }
            if (conv >= 97)
            {
                conv = (conv - 97);
            }
            else if (conv <= 90 && conv >= 65)
            {
                conv = (conv - 65);
            }
            if (plain[k] == ' ')
            {
                k++;
                printf(" ");
            }
            if (isupper(plain[k]))
            {
                if (key >= n)
                {
                    key = (key - n);
                }
                printf("%c", (( plain[k] - 'A' + conv) % 26) + 'A');
            }
            else if (islower(plain[k]))
            {
                if (key >= n)
                {
                    key = (key - n);
                }
                printf("%c", (( plain[k] - 'a' + conv) % 26) + 'a');
            }
         }
    }
    printf("\n");
}  

1 Answer 1

2

I figured out my issue, the string checks were being double looped on second cycle, fixed the issue by adding constant variable shifts for the key string.

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

int main(int argc, string argv[])
{
int i = 0;
string s = argv[1];
//recieve first argument
if (argc != 2)
{
    printf("Key not provided, try again.\n");
    return 1;
    }
    //check to see if argv[1] is alphabetical
    if (isalpha(s[i]))
    {
      for (int n = strlen(s); i < n; i++)
      {
          //printf("Current int: %c converted to: %i\n", s[i], s[i]);
      }
       // printf("Alphabet check passed...Proceeding...\n");
       //printf("%s\n", s);
    }
    else
    {
        //non-alphabetical key entered
        printf("Key is not alphabetical\n");
        return 1;
    }
    //ask for plaintext
    printf("plaintext: ");
    string plain = get_string();
    //ciphertext
    printf("ciphertext: ");
    s = argv[1];
    i = 0;
    int conv = s[i];
    if (s != NULL)
    {
        //run through plaintext string and key string
        for (int k = 0, x = strlen(plain), n = strlen(s); k < x; k++)
        {
            //running through key string checking strlen
            if (i < n)
            {
                conv = s[i];
                i++;
            }
            //once at the end of string set key to [0] and shift int
            else if (i >= n)
            {
                conv = s[0];
                i = 1;
            }
            //if upper/lower convert to 0-26
            if (conv >= 97)
            {
                conv = (conv - 97);
            }
            else if (conv <= 90 && conv >= 65)
            {
                conv = (conv - 65);
            }
            //if space print space shift int in plain string
            if (plain[k] == ' ')
            {
                k++;
                printf(" ");
            }
            if (isupper(plain[k]))
            {
                printf("%c", (( plain[k] - 'A' + conv) % 26) + 'A');
            }
            else if (islower(plain[k]))
            {
                printf("%c", (( plain[k] - 'a' + conv) % 26) + 'a');
            }
         }
    }
    printf("\n");
    return 0;
}

You must log in to answer this question.

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