this post was submitted on 18 Nov 2023
6 points (100.0% liked)

Programming

17366 readers
508 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 

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)
    { ...
top 5 comments
sorted by: hot top controversial new old
[–] jjagaimo@lemmy.ca 7 points 11 months ago* (last edited 11 months ago) (2 children)

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

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

[–] lolcatnip@reddthat.com 3 points 11 months ago* (last edited 11 months ago)

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

[–] milon@lemm.ee 2 points 11 months ago* (last edited 11 months 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.

[–] canpolat@programming.dev 3 points 11 months ago* (last edited 11 months ago)

Please consider posting language specific questions to language specific communities in the future. For example, !c_lang@programming.dev

[–] breadsmasher@lemmy.world 1 points 11 months ago

what language? needs more context