0

Here's my question: Below in the beginning of my code, I am hoping to assign argv[1] as a string k. Then I want to subtract the value of 'A' or 'a' from each char, depending on its case. After all of this I hope to use this altered string to change the plaintext into encoded text.

Should I make a new string for this instead? I know that I shouldn't be using GetChar() but rather some other thing but I don't know what that would. I am trying to assign the new value of x to k[i].

Thanks for any help!

int main(int argc, string argv[])
{
int n = 0;
char x = 'a';
// checks if there is only one argument
if (argc == 2)

 // declare array of chars that is the key
 {
    string k = argv[1];
    n = strlen(k);

    for (int i = 0; (i <= n + 1) ; i++)
    {
        if (isalpha(k[i]) || k[i] =='\0')
        {
            if (isupper(k[i]))
            {
                x = (k[i] - 'A');
//TODO: Change this so that k[i] is replaced with its new value of k[i] - 'A'
                k[i] = GetChar();

            }
            if (islower(k[i]))
            {
                x = (k[i] - 'a');
                k[i] = GetChar();

            }
       printf("Your key is %s\n", k); 



//int key = (0 % strlen(k));
            printf("Type your plaintext.\n");
            string plain_text = GetString();
// check if alpha and then increase
            for (int j = 0, n = strlen(plain_text); j < n; j++)
            {
                if(isalpha(plain_text[j]) && isupper(plain_text[j]))
                {
                    printf("%c", ((plain_text[j] - 65 + k[j]) % 26) + 65);
                }
                if (isalpha(plain_text[j]) && islower(plain_text[j]))
                {
                    printf("%c", ((plain_text[j] - 97 + k[j]) % 26) + 97);
                }
                else if (isalpha(plain_text[j]) == false )
                {
                    printf("%c", plain_text[j]);
                }
                return 0;


            }
            }

1 Answer 1

1

There are a number of problems here, and they are interacting, making it rather complex to debug. In no particular order, here are some of them.

You are pointing k at argv[1], the key for encoding the plaintext. Soon after, you have k[i] = GetChar(); which is asking the user for input, a new character, and thus overwriting the key!

You are also asking for the string to be input one character at a time. The program spec says to take in an entire string, not a single character.

It looks like you have some serious mismatches on curly braces, or they weren't transcribed correctly.

You have extra prompts that you are printing. check50 expects exact input and output, so anything extra will be rejected. Reread the spec for exactly what is expected.

There may be other issues, but you need to straighten these issues out first. This will at least get you going.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

2
  • Thanks for your response. I know that the GetChar() is wrong. I am trying to turn the key, that was input as a string to be transformed into the numbers 0-25. In other words, the user gives a string. Then I want to look at each letter and turn it into a value that I can add to the plaintext later.
    – RLSK
    Aug 4, 2015 at 23:01
  • My question is, how do I replace each letter one at a time. I know that GetChar is not correct. But how can I reassign the value that I have subtracted?
    – RLSK
    Aug 4, 2015 at 23:01

You must log in to answer this question.

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