0

I don't understand why my code for pset2 isn't working. The goal is to use a shift key, given by the user, and then shift all the letters in a string, also given by the user, that amount of spaces, in whats known as a Caesar Shift. For my code I used a for loop for the length of the string. Then, if the iith iteration is between A and Z then it will check to see if when the shift is added to the integer version of the iith iteration, it will over flow. If so, it wraps around to the beginning. It does this for the a to z characters as well. It's hard to explain without being complicated, but I don't know what isn't working. Whenever I enter 3 for k, and the string "Cameron", all I get is "CCCCCCC". Below is my code:

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

    int letter;

    int main(int argc, char* argv[]) {

        // Returns error if the command line arguments are not up to specifications
        if (argc < 2 || argc > 2) {

            return 1;

        // Else runs the cypher as usual
        } else {

            // Defines the variable the is used for the cypher shift
            int k = atoi(argv[1]);

            // Gets original text intended to be encrypted
            char* oText = GetString();
            int txtLength = strlen(oText);

            // Encrypts text
            for (int i = 0; i < txtLength; i++) {

                // If the character is a space, ignores it
                if (oText[i] == ' ') {
                    letter = 32;

                // Else continues encrypting
                } else {

                    if ((int)oText[i] >= 65 && (int)oText[i] <= 90) {
                        if ((int)oText[i] + k > 90) {
                            letter = (int)oText - (26 - k);
                        } else {
                            letter = (int)oText + k;
                        }
                    } else if ((int)oText[i] >= 97 && (int)oText[i] <= 122) {
                        if ((int)oText[i] + k > 122) {
                            letter = (int)oText - (26 - k);
                        } else {
                            letter = (int)oText + k;
                        }
                    } else {
                        letter = (int)oText;
                    }
                }

                // Prints the text
                printf("%c", (char)letter);
            }

            // Prints new line
            printf("\n");

            // Returns that there were no errors.
            return 0;

        }
    }
1

In your maze of if and else if statements, you are calling your oText a lot. As you probably know, this is an array. What do you think will happen when you use (int) array? I mean, you can't make an integer out of an array right? Maybe access a value insiiiide that array?

I hope that helps! (If it did, make sure to mark this question as answered, I

6
  • Bob(?) is right, you have to select a particular array element. As for the casting of array elements as (int) types, it doesn't hurt here, but it isn't necessary. C will allow you to treat any char as a 1-byte signed integer. So something like char foo = 'z' - 'y' + 'a'; is perfectly acceptable. (btw, the result would be 'b'.) Next, you should take a look at isalpha(), isupper(), and islower(). They would probably be really useful here. ;-)
    – Cliff B
    Feb 17 '16 at 0:33
  • Ok thank you, but just a quick question. My code now works with all of the oText's changed to "oText[(int)i]" and that makes all of the checks in check50 work except one. @MrBobJamesBob :( encrypts "barfoo" as "onesbb" using 65 as key \ expected output, but not " Feb 17 '16 at 1:43
  • but not.... what? My crystal ball is still in the shop.
    – Cliff B
    Feb 17 '16 at 20:57
  • This is the error message I get when I use check50 : "barfoo" as "onesbb" using 65 as key ---\ expected output, but not /n" @CliffB Feb 17 '16 at 22:27
  • If that's not a typo and you actually got /n, then you probably have a forward slash printing out instead of a backslash. But I'm betting it is a typo. Looking back at your code, I'd say that you got caught by the test that checks to see if you're handling keys > 52 (or 2 * 26). Seems like you could benefit from an understanding of modulo arithmetic.
    – Cliff B
    Feb 17 '16 at 22:55

You must log in to answer this question.

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