0

I'm trying to use the isalpha() function to check whether all characters of a string are letters or not. However, when I use the debug50 module, it looks like after the first character of the string, the function isalpha() stops working.

Here's my code, with the aim to get back the input word as a ciphertext where all letters rotate by 1 position:

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

int main(void)
{
// Prompt for the input code
    string plain = get_string("plaintext: ");
    char str[] = "";

    
    // Loop through all letters
    for (int i = 0, n = strlen(plain); i < n; i++)
    {
        // Check if it's a letter
        if (isalpha(plain[i]))
        {
            char ch = plain[i] + 1;
            strncat(str, &ch, 1);
        }
        else
        {
            char cha = plain[i];
            strncat(str, &cha, 1);
        }
    }

    // Output from algorithm
    printf("ciphertext: %s\n", str);
}

When I input the text "Hello", I only get back "i" as an output (instead of having the full word "ifmmp"). Would anyone know why, at i = 1, the isalpha() considers that letter "e" of "hello" is not alphabetical and jumps to the else section of my code?

Thank you very much for your help! Ben

1

There are a couple issues here.

First, there's a known issue with debug50. For certain library functions, if you "step into" the line of code with the function call, debug50 will try to step into the function itself and will go off in the weeds. In this case, you should use "step over" instead, to process that line of code by NOT skipping into the library function and instead moving on to the next line of code. Some of these issues have been fixed, but some may still exist. This may account for your debug issues, or it may not apply at all and the real problem is what follows.

Next is the use of the strncat() function. Remember, strings are immutable. That means that their length (the space allocated to the string) can't be changed. The target string must already have enough space allocated to it to hold the lengthened content of the source, including the end of string marker.

In this code, the target string, str, was created with a length of 1, so ONLY has enough space allocated for the end of string marker. Unfortunately, in these cases, if the code doesn't error out (I haven't tested strncat for this), it will overwrite whatever memory follows in physical memory, corrupting that data. Results become unpredictable.

The code needs to allocate enough space in the target string to hold the entire encoded input string, plus the EOS marker.

There may or may not be other issues. Hope this helps.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Thanks a lot Cliff! I changed char str[] = ""; to char str[strlen(plain)]; which seems to work much better! However I have a new issue which is weird when running the code below: Input: hello Expected output: ifmmp Output I get: Hӣifmmp When I use debug50, I don't get these weird characters before the "ifmmp", I don't know why I get that. Would you have any idea?
    – jamba
    Jun 22 at 21:39
  • Found my mistake! Further to setting up str, I need to initialize it by defining first string like that char str[strlen(plain)]; str[0] = 0;
    – jamba
    Jun 22 at 22:07

You must log in to answer this question.

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