0

include // isalpha, isupper, islower - functions needed for caesar --- c = (p + k) %26

include // Check program is ran with only 1 command line arg. Check to make sure all characters are digits.

include // Use ASCII - wrap around letter a - z.

include // Output plain text with no new line. prompt user for string using get_string.

include // Output ciphertext - do not change from uppcase to lowercase, preserve other characters - , . ! ' "'

include // finally print new line at the end

include /* ASCII - A = 65 | a = 97 | Z = 90 | z = 122 */

int main(int argc, string argv[]) { // argc is number of arguments to be passed into program. Only accept 2 arguments. Program name and 1 other. if (argc != 2) return 1;

// Converts strings input to an integer.
int key = atoi(argv[1]);

// If key is less than or equal to 0 = reprompt user.
if (key <= 0)
    return 1;

// Convert argv to an integer.
int argument = atoi(argv[1]);

// If input is a digit and is greater than 0, pass.
if (isdigit(argument) > 0)
    return 1;

string pText = get_string("Plaintext: ");

for ( int i = 0; i < strlen(pText); i++)
    {
        if (isalpha(pText[i]) && isupper(pText[i])) //
        {
        printf("ciphertext: %c", (((pText[i] - 65 + key) % 26) + 65));
        }
                else if (isalpha(pText[i]) && islower(pText[i]))
                {
                printf("ciphertext: %c", (((pText[i] - 97 + key) % 26) + 97));
                }
                else
                {
                    printf("ciphertext: %c", pText[i]);
                }
    }
printf("\n");
return 0;

}

I'm having an issue with my output. "ciphertext: " is needed to pass the check, when i include it in my print statement it prints something like this.. ciphertext: iciphertext: fciphertext: mciphertext: m. Without "ciphertext" included in the print statement, my code seems to work.

Any suggestions would be greatly appreciated!!

1

As you know you've created a for loop that iterates once for each character introduced by the user of your program so, if you don't want to print printf("ciphertext: ......." for every character, you should only print the character like: printf("%c", YOURCHARVARIABLE);.

1
  • Thanks I got it figured out!
    – Ctouch
    Apr 22 '20 at 21:11

You must log in to answer this question.

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