0

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;
                    }
8
  • What is capAlph? Is this an int array or a char array? If it is an int array of the ASCII values of the capital letters, then your condition will never be true because A is 41 and if text[i] also equals A, then 41 - 41 will equal 0, meaning that your if condition will not evaluate the two capital "A"s as equivalent. Unless I am misunderstanding your code's purpose or what capAlph is supposed to be. Jun 3 at 13:44
  • capAlph is array of chars, that I 've made manually of the CAP alpahabetical letters. and text[i] is the char element from the input. Jun 3 at 14:25
  • Also I 've tried the condition without subtracting the 'A', and it was never met too Jun 3 at 14:56
  • Yes, C stores chars as ints 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 declared capAlph, text, and your outer for loop? Copying your provided code and writing a basic char array for capAlph of {'A', 'B', 'C', 'D'} and then a basic array for text of {'A', 'D'} gives me expected results with your if condition changed to if(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. Jun 3 at 16:16
  • I 've edited my question. Jun 4 at 9:03
1

I have tested your code, including the new code you've added in your edit, and your conditions work when the arithmetic is removed. This means changing capAlph[p] == text[i] - 'A' to capAlph[p] == text[i] and smaAlph[p] == text[i] - 'a' to smaAlph[p] == text[i]. The arithmetic, as I explained in a comment, ensures that the two values will never be equal. I am not sure how you were/are testing to see if the nested code was being executed, but what I did was comment out your code inside the nested if blocks and add a single printf statement of upper alpha match found or lower alpha match found, depending on the condition, to confirm that they were indeed executing.

After fixing the above (or during your testing, depending on how you were approaching it), you might find that the nested code is no longer executed for repeated letters in text for the reason below...

The code within each nested if block modifies your alphabet arrays. capAlph[p] = (capAlph[p] + key) % 26; will change the original value of capAlph[p] to whatever is computed, making a repeated letter in text no longer equal the same index value of capAlph (a different p will need to be found for that letter). Because you are not taking in to account that the algorithm being performed on capAlph[p] will provide a different char value than already exists in your capAlph array, it is possible that capAlph[p] will be made to equal a value already present, removing the original character from the array.

What you need to do is find a way to perform your algorithm to create the ciphertext without modifying your capAlph and smaAlph arrays, which you are assuming to be consistent for your nested if statements to work.

Hope that helps and makes sense and helps. If it does, feel free to mark the answer as accepted by clicking the check mark beside it. If not, let me know and I will try to help further.

5
  • Thanks for your explanation. I 've tested the code using debug50 existing in CS50 ide, that's how I figured out that the nested condition was never entered. But I agree that I 've to change the code so the capAlph char is not modified, but I don't think this will be a way to fix the issue of the condition to be executed. Jun 4 at 12:23
  • Removing the - 'A' and - 'a' will make them execute. The other stuff was to prevent a possible problem later on, if you find that happening; essentially pointing out a highly-probable logic error. You can always post your entire code, but the only issue with your conditions is the subtraction. Jun 4 at 12:30
  • The condition was entered correctly, I 'll try changing my code to implement what we discussed above, and if there are further problems I will reach out. Thanks so much. Jun 4 at 13:02
  • Hello again :D, I 've changed the inner-most condition and edited the question to contain the edit. But, it seems not to help to much. So any thoughts would be so helpful :). Thanks. Jun 5 at 15:54
  • Also, while debugging, I noticed that the condition is entered twice. Jun 5 at 15:59

You must log in to answer this question.

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