when debugging, the inner-most if condition never entered. Whether the char is upper-text or lower text. I 'm using the same methodology in both cases.
// testing if the char upper text
if (isupper(text[i]))
{
// looping through the cap alphabet array
for (int p = 0; p < 26; p++)
{
// testing if the char matches the char in the array
// switcing from ASCII index to alphabet index
if (capAlph[p] == text[i] - 'A')
{
// applying the formula
capAlph[p] = (capAlph[p] + key) % 26;
// returning back to ASCII index
text[i] = capAlph[p] + 'A';
}
}
}
Edits
Declaring capAlph
:
// capital aphabets
char capAlph[26] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
Declaring the input text text
//getting normal text
string text = get_string("plaintext: ");
The outer for loop:
for (int i = 0; i < n2; i++)
{
// testing if the char alphabet
if (isalpha(text[i]))
{
// testing if the char upper text
if (isupper(text[i]))
{
// looping through the cap alphabet array
for (int p = 0; p < 26; p++)
{
// testing if the char matches the char in the array
// switcing from ASCII index to alphabet index
if (capAlph[p] == text[i] - 'A')
{
// applying the formula
capAlph[p] = (capAlph[p] + key) % 26;
// returning back to ASCII index
text[i] = capAlph[p] + 'A';
}
}
}
// testing if the char lower text
if (islower(text[i]))
{
for (int p = 0; p < 26; p++)
{
// looping through the cap alphabet array
// doing same as the CAP alphabet
if (smaAlph[p] == text[i] - 'a')
{
// switching to smaAlph index
smaAlph[p] = (smaAlph[p] + key) % 26;
// returning to ASCII index
text[i] = smaAlph[p] + 'a';
}
}
}
}
}
Edit Change in the inner-most condition
if (capAlph[p] == text[i])
{
// storing capAlph original value in a char
char lett = capAlph[p];
// applying the formula
capAlph[p] = (capAlph[p] + key) % 26;
// returning back to ASCII index
text[i] = capAlph[p] + 'A';
// switching capAlph[p] to its original value
capAlph[p] = lett;
}
capAlph
? Is this anint
array or achar
array? If it is anint
array of the ASCII values of the capital letters, then your condition will never be true becauseA
is41
and iftext[i]
also equalsA
, then41 - 41
will equal0
, meaning that your if condition will not evaluate the two capital "A"s as equivalent. Unless I am misunderstanding your code's purpose or whatcapAlph
is supposed to be.char
s asints
so please ignore any confusion my morning brain might have caused, though it did lead to the clarification I wanted. Can you add to your question how you have declaredcapAlph
,text
, and your outer for loop? Copying your provided code and writing a basicchar
array forcapAlph
of{'A', 'B', 'C', 'D'}
and then a basic array fortext
of{'A', 'D'}
gives me expected results with your if condition changed toif(capAlph[p] == text[i])
, so the problem does not seem to be with the code here, if removing the- 'A'
does not improve your situation.