2

Pseudocode explanation of what I am attempting to do.

  1. Convert a character array into an integer array

  2. Iterate through each integer in that array and add q to it, unless that integer + q exceeds an upper bound. If it exceeds that number, return the modulus and add the modulus to a lower bound.

  3. print the converted integer array in its ASCII sequence using %c.

Here is the example:

int main(void)
{
    char char_message[] = "abcyzABCYZ";
    int q = 10;
    int i;
    int message[10];
    int n = strlen(char_message);

    for(i = 0; i < n; i++) {
        message[i] = atoi(&char_message[i]);
    }

    for(i = 0; i < n; i++) {
        if(message[i] <= 90) {

            if (message[i] + q <= 90) {
                message[i] = message[i] + q;
            }
            else
                message[i] = 65 + (message[i] % 90);
        }
        else if(message[i] >= 97) {

            if (message[i] + q <= 122) {
                message[i] = message[i] + q;
            }
            else
                message[i] = 97 + (message[i] % 122);
        }
    }

    for(i = 0; i < n; i++) {
        printf("%c", message[i]);
    }

    return 0;
}

EDIT: Below is a second attempt at this problem --------------------------

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

    if(argc != 3) {
        printf("Enter an integer followed by a string \n\n");
        return 1;
    }

int i;
int offset = atoi(argv[1]);
char **p_message;
p_message = &argv[2];
char encrypt[strlen(*p_message)];

printf("You Entered: %d, %s \n", offset, *p_message);

for(i = 0; i < strlen(*p_message); i++)
    {
        encrypt[i] = ((*p_message[i] + offset) % 26);
    }

for(i = 0; i < strlen(*p_message); i++)
{
    printf("%c", encrypt[i]);
}


return 0;

}
Anthony O
  • 622
  • 7
  • 26
  • 1
    Change your printf line in your loop to this: `printf("%d %c\n", message[i], message[i]);` Now, debug your program and find out why all those odd values are there. That `atoi` shouldn't even be in this program. – WhozCraig Sep 04 '16 at 19:55
  • Indeed: `message[i] = char_message[i];` If you use `atoi` it will return `0` since you are applying it to (the rest of) an array that contains only alphas. And please replace all your magic numbers with `'A'` or `'a'` or `'z'` etc, which have `int` values (assuming ASCII). – Weather Vane Sep 04 '16 at 20:04
  • 1
    I think you are applying your modulus incorrectly. There should be `% 26` in there, applied to offsets from `'A'` or `'a'` and then added to the base. – Weather Vane Sep 04 '16 at 20:11

1 Answers1

1

I'm not doing your homework for you, but it is obvious that two things are clearly wrong.

  • Your use of atoi is incorrect. It should not be here at all.
  • Your calculation of the resulting enciphered character is wrong as well. The modulus placement is wrong in two different locations.
  • Your use of "magic numbers" in this code is rampant making it much, much more difficult to read than need-be. Avoid using magic numbers.

The following "enciphers" your test string via a simple forward scanning loop, output each resulting character one at a time. I've left the storage to a separate int array for you to handle. Of note the first if-block is expanded out statement by statement so you can see what is going on one step at a time. The second (lower case handling) is done in a single expression. Other than different ranges, the two methods of calculation are equivalent.

Note: this only works on platforms where character ranges A..Z and a..z are continuous. The language standard makes no enforcement of this; it only enforces it for digit characters 0..9. Thus, don't blame me if you run this on an AS/400 or OS/390 (both EBCDIC platforms) and it doesn't work.

#include <stdio.h>
#include <string.h>

int main()
{
    char message[] = "abcyzABCYZ";
    const int q = 10;

    puts(message);

    for(const char *p = message; *p; ++p)
    {
        int c = (unsigned char)*p;

        if (c >= 'A' && c <= 'Z')
        {
            c -= 'A';
            c += q;
            c %= ('Z' - 'A' + 1);
            c += 'A';
        }

        else if (c >= 'a' && c <= 'z')
            c = ((c - ('a' - q)) % ('z' - 'a' + 1)) + 'a';

        // else nothing. keep as-is

        fputc(c, stdout);
    }
    fputc('\n', stdout);

    return 0;
}

Output

abcyzABCYZ
klmijKLMIJ
Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Thank you, I have perused your code and made another attempt using an approach different than your example and my previous attempt where I have a segmentation fault – Anthony O Sep 04 '16 at 21:54
  • Would you care to let me know what you think? – Anthony O Sep 04 '16 at 21:59