0

I feel i almost got this locked in but my keyword doesn't seem to to work. It isn't rotating through the keyword. please tell me what i did wrong my brain hurts lol.

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc,string argv[])
{

string sKeyWord = argv[1];                   
int iKeyLength = strlen(sKeyWord);           
int iKey[iKeyLength];                        
string sPlainText = "";
int counter = 0;
int iAccept = 0;
do
{
    if(argc != 2)
        {
            printf("Invalid argument! Please enter program name and keyword.\n");
            return 1;
        }
    else if(argv[1])
        {
        for(int i = 0; i < iKeyLength; i++)
            {
                if (!isalpha(argv[1][i]))
                {
                    printf("Invalid entry, please use letters only.\n");
                    return 1;
                }
                else
                {
                    iAccept = 1;
                }
            }
        }    
}while(iAccept == 0);


for(int i = 0; i < iKeyLength; i++)           
    {
        iKey[i] = toupper(sKeyWord[i]) - 65;     
    }

sPlainText = GetString();                    
int iPlainText = strlen(sPlainText);        


for(int j = 0; j < iPlainText; j++)
    {
        if(!isalpha(sPlainText[j]))
            {
                printf("%c",sPlainText[j]);
                counter++;
            }
        if(islower(sPlainText[j]))
            {
                printf("%c",((((sPlainText[j] - 97) + iKey[(j - counter)%iKeyLength])%26)+ 97));
            }
        if(isupper(sPlainText[j]))
            {
                printf("%c",((((sPlainText[j] - 65) + iKey[(j - counter)%iKeyLength])%26)+ 65));
            }

    }
printf("\n");

return 0;
}

When I run what I have, it works fine as far as I can tell except for it only uses the first letter of the keyword instead of rotating through it.

1 Answer 1

0

The code doesn't increment k, so it never changes from 0. A for loop has three clauses, initialization, test and increment. The code initializes k in the first or initialization clause. After the initialization, the initialization clause doesn't have any effect on anything. It doesn't cause k to change as i changes. K needs to be updated somewhere inside the loop or in the incrementation clause. Just remember that k will only be updated when a letter is actually encoded. If it's linked to i, it will get out of synch.

Some side comments: if((argc == 1) || (argc < 2) || (argc >2)) Why these three tests? If the first test is true, then the second is true. All three could be replaced by argc != 2, simplifying the code.

if (isalpha(keyWord[i]) == false) can be simplified to if (!isalpha(keyWord[i]) ).

Looks like you could benefit from understanding the NOT operator, !.

And what happens if the key is uppercase?

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

2
  • Thanks for the help cliff. I got everything nailed down except now program does not complain when not given a second arg. Instead I get a segmentation fault. I have updated the OP to the new code. it works fine with 2 args and complains when 3 or more are given
    – Firecore
    Commented May 1, 2016 at 1:35
  • The problem is that you are assigning argv[1] to another variable before you check to see that it exists. Instant seg fault.
    – Cliff B
    Commented May 1, 2016 at 3:15

You must log in to answer this question.

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