Per the manual page for isalpha()
isalpha()
checks for an alphabetic character; in the standard "C" locale,
it is equivalent to (isupper(c) || islower(c))
.
In other words, isalpha()
basically returns true
if the passed in char
is an alphabetical character. Otherwise it returns false
. Meanwhile,
if (isalpha(c))
{
// c is alphabetical
if (isupper(c))
{
// c is uppercase
}
else
{
// c is lowercase
}
}
else
{
// c is non-alphabetical
}
Also, how can I check again and again the length of my word and the
text? If the text is longer than the word, I get an error.
Calculating the index of the current char
in the keyword using the remainder operator is one way of achieving that. Given the expression a % b where a and b are integers, mathematically, the result would be >= 0 && < b.
Since we're supposed to increase the index of the current char in the keyword by one as we encipher the current plain character in the plain text, if the length of the number of plain characters in the plain text is greater than the length of the keyword, eventually, the index of the current char in the keyword will become equal to (and exceed, if we continue) the length of the keyword and we'd wind up with invalid index(es).
Taking the remainder of dividing the index of the current char in the keyword by the length of the keyword will wrap the index back to 0 once it's equal to the length of the keyword.
Edit #0: after looking at the code, I could find the following problems
In general, it's not a good practice to have something like
if (condition)
{
// do nothing
}
else
{
// do something
}
you could directly have something like that instead
if (!condition)
{
// do something
}
The cases that should be covered are
a. if the current character in the plain text is an uppercase char
b. if the current character in the plain text is a lowercase char
what you're covering though is
a. if the current character in the plain text is an uppercase char
- if the current character in the keyword is an uppercase char
b. if the current character in the plain text is a lowercase char
- if the current character in the keyword is a lowercase char
c. if the current character in the plain text is a lowercase char OR (the current character in the keyword is an uppercase char OR a lowercase char)
Now, you claim that your cases work well, but they're really difficult to read and understand.
You shouldn't calculate the index of the current char in the keyword in terms of i
because it always increases whether the current char in the plain text is an alphabetical char or not. Rather, you should calculate it in terms of j
.