0

So im new to this site and im pulling out my hair trying to figure this last issue out with my program. Ive got my characters shifted and my ciphertext printed, yet i cannot for the life of me print out non-alphabetic characters in my output for ciphertext. Everytime I add in a new print line outside my isupper and islower conditions, it meshes both prints together (the ciphertext and plaintext) and then it will print my non-alphabetic characters.

{
    printf("ciphertext: ");
    for (int i = 0, n = strlen(p); i < n; i++)
    {

        if (islower(p[i]))
        {
            printf("%c", (((p[i] - 97 + key) % 26) + 97));
        }

        if (isupper(p[i]))
        {
            printf("%c", (((p[i] - 65 + key) % 26) + 65));
        }

     printf("%c", p[i]);

    }
    printf("\n");
    return 0;
}

}

1 Answer 1

1

The program is "always" doing something (printing) instead of "conditionally" doing that thing.

The pseudocode is something like:

Is character lower case? print ciphered character.
Is character upper case? print ciphered character.
Print (plain text) character.

Each print should be based on some condition. One way to fix it would be to add a "condition" for the printf("%c", p[i]);. You may find isalpha() of use.

Since a character will only return true for one of the three conditions, you should get one "output" for each character in the plain text.

This can also be accomplished using if/ else if/ else if structure.

3
  • I was having issue using the isalpha() command earlier because i still need to keep special character such as spaces, exclamation points, commas, etc. and wouldn't that return false, or just drop, if present?
    – Chris
    Mar 10, 2017 at 17:23
  • wait so i think i just fixed my problem, but I was under the impression that in C so long as the if statements were outside of one another they automatically were treated as if/ else if/ else if/ structures???
    – Chris
    Mar 10, 2017 at 17:33
  • Regardless, right now my program is working because of the structure which seems so obvious but is blowing my mind right now that its virtually all I needed. I cannot thank you enough!
    – Chris
    Mar 10, 2017 at 17:41

You must log in to answer this question.

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