0

My result is wrong ! when I enter a word in plaintext, it just print 2 letters for infinity. Please help me understand how I can print ciphertext and the answer correctly please. Here is my code :

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

int main(int argc, string argv[])
{ 
     //select the second string which is the key 
    string key = argv[1];

     //only 1 string is allowed next to the command
    if (argc != 2) 
    {
        printf("error\n");    
        return 1;        
    }

         //convert the string to an integer
        int k = atoi(key);

         //key value should be positive 
        if (k > 0 )
        {
             //make plaintext input of user

            {
                printf("plaintext: ");

            }
             string plntxt = get_string();
             if (plntxt != NULL)

            { 
                printf("ciphertext: ");
            }
              //make plaintext encrypted  
              //use key input to encrypt 
              int c = 0;
              for (int i = 0, n = strlen(plntxt); i < n; n++)   
              {
                  //make sure that string is letter
                 if (isalpha(plntxt[i]));
                 {
                      //preserve the lower case 
                     if (islower(plntxt[i]));
                     {
                          // convert the asc II to alpha index 
                         int p = plntxt [i] - 97; 
                          //encrypted
                         c = (p + k) % 26;
                          // convert the encrypt to ascII
                         int letter = c + 97;

                         {
                             printf("%c", (char)letter);
                         }

                     }

                      //preserve the upper case 
                     if (isupper(plntxt[i]));
                     {
                          // convert the asc II to alpha index 
                         int p = plntxt [i] - 65;
                          //encrypted
                         c = (p + k) % 26;
                          // convert the encrypt to ascII 
                          int nletter = c + 65;
                         {
                              printf("%c", (char)nletter);
                         }
                     }


                     printf("%c", (char)plntxt[i]);
                 }
              }
        }

   {
       printf("\n");
   }
   return 0;
}
0

Why do you print letter and nletter at the end? With each iteration of the for-loop, these variables' values will change, so you'll only be printing the last value. One possible way to fix this is to put the printf statement below int letter = c + 97; and below int nletter = c + 65;. If you do this, it'll print every letter, whether it's capital or lowercase.

What happens if you get the input "my name is bob" with a key of 2? The ciphertext should be oa pcog ku dqd. Notice that the non-alphabetic characters (in this case, the spaces between the words) are still being printed. Now look back at your code. You have an if statement that checks and handles alphabetic characters, but what about non-alphabetic characters? You might want to add an else statement to your if(isalpha(plntxt[i]) that handles these non-alphabetic characters.

Also, you don't need the char[letter] and char[nletter] in your printf statement. Because you put %c as the variable placeholder, it will print letter and nletter as characters. For example, in the code:

int number = 97;
printf("%c", number);

It will print the letter a even though the variable passed in is declared as an int.

Edit:

Order is very important in programming. In caesar.c, you want to print plaintext: into the terminal and then have the user enter a value. Let's take a look at your code.

//make plaintext input of user
string plntxt = get_string();
if (plntxt != NULL)
{
     printf("plaintext: \n");    
     return 0;
}

You ask the user for a string and then go on to check if the string is NULL. Okay, good so far. But, wait, aren't you supposed to print plaintext: before you get the string? Remember, this is what the output should look like:

$ ./caesar 13
plaintext:  HELLO
ciphertext: URYYB

Also, you want to print ciphertext: once and then print the letters. With your current printf statements, you'd print ciphertext: every time you encrypt a letter. Why do you also have extra brackets {} around your printf statements? The printf statements aren't in a function, if/else statement, or for-loop, so there isn't a need for these brackets.

I recommend that you take another look over the specification again. (Although, if you find the English difficult to understand, I'd be happy to help you understand what you needed to do.)

So far - nice job! If you still have issues, comment below.

9
  • Thank you very much. for the else, yes, i put at the end which is : printf("%c", plntxt[i]); but when I add the letter else above the print, i have an error, so I deleted. here are the wrong results I have with my code that I will edit that you can see now that still wrong workspace/pset2/ $ ./caesar a plaintext: ~/workspace/pset2/ $ ./caesar -2 plaintext: ~/workspace/pset2/ $ ./caesar boo 2 ami error ~/workspace/pset2/ $ ./caesar a plaintext: ~/workspace/pset2/ $ – Genevie Apr 24 '17 at 1:45
  • Could you edit your question with the errors so I can see them more clearly? Thanks. Also, for future reference, click {} to properly format code. :-) – SuperNovaCoder Apr 24 '17 at 1:49
  • thanks so much. I already correct the k which was >1 and not <1. did i add the error correctly ? what do you mean by future reference, click {} to properly fomat code ? PS I m a french speaker so sometimes language is a barrier to communicate, sorry. – Genevie Apr 24 '17 at 2:04
  • No worries! What I meant was that when you just paste your code into the text box, it can be hard to read. Highlight (or select) all of your code and then click {}. It will properly indent your code to make it easier to read. Yes, you added your error correctly. I'll take a look at your code and then I'll edit my question. :-) – SuperNovaCoder Apr 24 '17 at 11:28
  • Alright, I edited my question. The link I gave you in the edited part of my answer should help. If you have trouble understanding anything, please tell me. I'll try and break it down for you. :-) – SuperNovaCoder Apr 24 '17 at 11:46

You must log in to answer this question.

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