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