This is in C language. When I call rotate() in main, the function returns false for isalpha() even though the string entered for plaintext uses alphabetic characters. Perhaps it’s identifying an alphabetic character by its ASCII value (‘A’ = 65)? I tried to test that out and used (char) with the letter variable in rotate() but it didn’t change anything.

PORTION OF MAIN

string plaintext = get_string("plaintext:  ");

    int length = strlen(plaintext);
    char ciphertext[length];

    for (int i = 0; i < length; i++)
    {
        ciphertext[i] = rotate(plaintext[i], key);
    }

ROTATE FUNCTION

char rotate(char letter, int key)
{
    if (isalpha(letter) == true)
    { ...
  • @[email protected]
    link
    fedilink
    English
    15
    edit-2
    1 year ago

    isalpha documentation:

    Return value
    
    Non-zero value if the character is an alphabetic character, zero otherwise.
    

    You should be either checking for not equal to 0 instead of true, as its not necessarily guaranteed to be 1 ~= true, or removing the comparison entirely

    Also make sure that your loop condition is < and not “& lt” without the space unless that’s a weird formatting issue

    For more information, make sure to check the documentation for the standard library functions

    • @pHr34kY
      link
      151 year ago

      I would drop the “== true” entirely. C will evaluate any nonzero int as true in an “if” statement.

    • @[email protected]OP
      link
      fedilink
      4
      edit-2
      1 year ago

      Ah ha! Yes, I did check the docs but I think I just glanced over that portion. Be more careful next time. Now that I took another look at the other ctype.h functions, they all return 1 or 0. I think I confused equivalent python built-in functions as those evaluated to true/false. The < is a less than sign but it seems it doesn’t render correctly on Lemmy.

    • @[email protected]
      link
      fedilink
      English
      2
      edit-2
      1 year ago

      Never, ever write “== true” or “== false” unless it’s absolutely necessary. In C that’s pretty close to never.

  • @breadsmasher
    link
    English
    4
    edit-2
    1 year ago

    what language? needs more context

    portion of main and function are just titles for me, if that is supposed to contain code

    • @[email protected]OP
      link
      fedilink
      31 year ago

      Sorry. It’s in C. Updated post. Yes those are titles. I just included the relevant portions rather than the entire code.