-1

I'm working my way through an online class teaching me how to code. I'm very new to this and have been slowly making my way through this class. I've run into an issue with the vingenere cipher. It doesn't iterate the key through the whole input.

Edit: the key should iterate through the user input and when it reaches the end of the key, loop back and begin again. The key should also skip over any special character(!@#" ",etc)

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

int main (int argc, string argv[])
{
    if(argc !=2)
    {
        printf("please put in command line argument: example - ./vigenere command\n");
        return 1;
    }
    string key = argv[1];
    int keylength = strlen(key);
    for (int i=0;i<keylength; i++)
    {
        if(!isalpha(key[i]))
        {
            printf("please make sure command is letter only. Please no numbers or special characters!\n");
            return 1;
        }
    }
    string input = GetString();

    for (int i=0, k=0; i<keylength; i++)
    {
        if(isalpha(input[i]))
        {
            if(isupper(input[i]))
            {
                input[i]=((input[i]-'A')+(key[k%keylength]))%26+'A';
            }
            else
            {
                if(islower(input[i]))
                {
                input[i]=((input[i]-'a')+(key[k%keylength]))%26+'a';
            }
        }
    }
}
printf("%s\n",input);
return 0;
}

I know string is not normal, but it's included in the header to help with new students. I guess we learn more as the class progresses.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3658638
  • 1
  • 1
  • 1
  • 4
  • 1
    What type is `string`? – timrau May 20 '14 at 23:30
  • essentially from what I understand, it's char* https://manual.cs50.net/library/ – user3658638 May 20 '14 at 23:47
  • You should really discourage your teachers from doing this `string` thing with C. C doesn't have a proper string type and it's not a good idea to pretend that char* is one. Furthermore, it's not idiomatic, so most C programmers will not understand it at first sight. – JeremyP May 21 '14 at 01:40
  • Hey Jeremy, I don't disagree with you, but considering this is an online class, with thousand of student, I don't think my concerns really matter. I think the idea is to slowly ease you into coding. On the subject of the code at hand, do you have any suggestions with how to make sure the cipher skips over special characters and keeps its place within in key. – user3658638 May 21 '14 at 01:53

1 Answers1

1

You didn't change k in your for loop. And indeed I don't think you need k at all. And your loop only iterate through the length of key instead of the length of input.

int inputlength = strlen(input);
for (int i = 0; i < inputlength; ++i) {
    if (isupper(input[i]))
        input[i] = ((input[i]-'A') + (key[i%keylength])) % 26 + 'A';
    /* ...                                ^ Use i here */
}

Regarding the issue that when the key is b and input is A, you must adjust the key.

input[i] = ((input[i]-'A') + (key[i%keylength]-'a')) % 26 + 'A';

To skip input special characters,

int inputlength = strlen(input);
for (int i = 0, k = 0; i < inputlength; ++i) {
    if (isupper(input[i]))
        input[i] = ((input[i]-'A') + (key[(k++)%keylength])) % 26 + 'A';
    /* ...                                 ^^^ */
    else if (islower(input[i]))
        input[i] = ((input[i]-'a') + (key[(k++)%keylength])) % 26 + 'a';
}
timrau
  • 22,578
  • 4
  • 51
  • 64
  • Okay, I get the first part. I may have been working on this for faaaar too long. I don't know how I missed that. I've updated it so it will iterate through the whole input now. I'm still having an issue with the math. when the key is b and the input is A, it comes out as U when it should be B. Also, the code iterates over special characters. so if the input is bc and the input is A!A the out put is U!U when it should be U!V (based on the bad math it's currently doing) – user3658638 May 21 '14 at 00:08
  • That worked great. Now on to the last part which is to skip special characters and preserve the spot within the key. – user3658638 May 21 '14 at 02:22
  • @user3658638 edited again for skipping special input characters. – timrau May 21 '14 at 05:45
  • It's so simple. I have been working on this for a very long time. Thank you so much – user3658638 May 21 '14 at 22:03