1

In C, my program will encipher a user input by an arguement integer, and output new characters. Everything works except what seems like 5 characters (j-o), they print the new character but also include a space after. Why would these specific characters do this? Any help would be nice.

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

//alphabetical index
int NUMERIN[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 24, 25};
char ALPHAIN[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

int main(int argc, char * argv[1])
{
    //check number of command line inputs
    if (argc == 2)
    {
        int key = atoi(argv[1]);

        //check that key is non negative
        if (key > 0)
        {
             //variable index
            int c;
            string plaintext = get_string("plaintext: ");
            int fin = strlen(plaintext);
            int index;
            char ciphertext;

            printf("ciphertext: ");

            //Loop to check all input characters
            for (c=0; c < fin; c++)
            {
                //check plaintext for letters to encipher
                if isalnum(plaintext[c])
                {
                    //loop for uppercase plaintext characters
                    if isupper(plaintext[c])
                    {
                        plaintext[c] = tolower(plaintext[c]);

                        //match character to index, encipher and print new character
                        for (index = 0; index < 26; index++)
                        {
                            if (plaintext[c] == ALPHAIN[index])
                            {
                                plaintext[c] = NUMERIN[index];
                                ciphertext = (plaintext[c] + key) % 26;
                                ciphertext = ciphertext + 65;
                                ciphertext = toupper(ciphertext);
                                printf("%c", ciphertext);
                            }
                        }
                    }

                    //loop for lowercase plaintext characters
                    if islower(plaintext[c])
                    {
                        //match character to index, encipher, and print new character
                        for (index = 0; index < 26; index++)
                        {

                            if (plaintext[c] == ALPHAIN[index])
                            {
                                plaintext[c] = NUMERIN[index];
                                ciphertext = (plaintext[c] + key) % 26;
                                ciphertext = ciphertext + 97;
                                printf("%c", ciphertext);
                            }
                        }
                    }
                }

                if isspace(plaintext[c])
                {
                    printf(" ");
                }

                if ispunct(plaintext[c])
                {
                    printf("%c", plaintext[c]);
                }

            }

        //complete encipher
        printf("\n");
        return 0;
        }

        //error for non-positive key input
        else
        {
            printf("Usage: %s key\n", argv[0]);
            return 1;
        }
    }

    //error for wrong number of command line inputs
    else
    {
        printf("Usage: %s key\n", argv[0]);
        return 1;
    }

}

Also, is my code readable? Should I use less specific variable names? I haven't cleaned it all up yet, but am curious if I'm on the right track as far as quality goes. Thanks.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JLAc91
  • 35
  • 3
  • 1
    I suggest putting a few `else` statements in the code. For example `if (isupper(plaintext[c]))` lowercases the character in-place, prints something, but then `if (islower(plaintext[c]))` becomes true, which also prints something. – Weather Vane Jan 13 '21 at 19:11
  • The code will not compile. Please post the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest *complete* code that shows what you have tried. The best way to do that is by copy/paste. – Weather Vane Jan 13 '21 at 19:13

0 Answers0