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

int main( int argc,string argv[]){

    int k;

    if (argc == 2) {

        k = atoi(argv[1]);
    }
    else
    {
        printf("Usage: ./caesar k");
    }
        string p = get_string();

       printf("Plaintext: %s\n",p);

        char c[100]; 
        for(int i = 0,n = strlen(p);i < n;i++)
        {

        if(islower(p[i]) && isalpha(p[i]))
        { 
             c[i] = (p[i] - 'a'  + k) % 26 + 97;
        }    

         if(isupper(p[i]) && isalpha(p[i]))
         {
             c[i] = (p[i] - 'A' + k) % 26 + 65; 
         }
          {  
           printf("%c",c[i]); 
          }  

        }   
      }  
}  
  printf("\n");  

  return 0;
}

In my printf statement where I want to print my Ciphertext, the printf function's output is behaving different to what I have anticapated. here you can see the output

output without Ciphertext before %c enter image description here

1
  • You are printing CipherText: for every character you encrypt. I would put it outside for loop (before for loop actually).
    – vadasambar
    Sep 26 '17 at 15:54
0

You seem to build a new, "encrypted" string from input.

You would then print that string as a whole after the loop.

An alternative is to print the ciphertext: before the loop (so it's printed once), and within the loop print the encrypted characters one at a time, later print a newline after the loop. That one would not require storing the whole ciphertext.

BTW, your processed string does not seem to be null terminated. Also, what happens if your input string is 100 characters or longer? Using an arbitrary buffer size is not a good idea, especially not if you don't enforce this maximum length (e.g. truncate the string if it's too long, or throw an error, but there's no such thing in the specification).

5
  • Yes you are absolutely right about the option I have chosen for the meantime is anything but nice, thank you for pointing that out to me again. That being said, it has no Aug 24 '17 at 13:13
  • no direct effect on the problem I wanted to demonstrate, the printf statement behaves not as I expected. I.e. if I write 'Ciphertext' before %c it behaves, in the manner I showed earlier. If I leave the wording 'Ciphertext' out and just put the %c, the cipher behaves as I want it to, but of course then I miss the 'Ciphertext' declaration in front of it. I can not understand why this is the case. Aug 24 '17 at 13:27
  • As I wrote in my answer, print ciphertext: before the loop. And a single '\n' after the loop.
    – Blauelf
    Aug 24 '17 at 13:35
  • I am sorry, I took your answer print the Ciphertext to literal or maybe not enough, as I couldn't understand why you said that, and it didn't make sense to me. So I tried to actually print the ciphertext before the loop which of course didn't work and made no sense. I then stared at your wording again and realised what you mean. And the result blew me away, I have to say, I am so amazed and fascinated by C and programming. One last thing, as I have a string of Chars here, it is automatically NUL-terminated, am I right in my theory?. If yes, should I rely on this?. Thank you for your input. Aug 25 '17 at 12:46
  • It's only null terminated if you make it so. Certain string functions like strcpy add the null terminator, others (like strncpy) add null terminator under certain circumstances (strncpy fills the remaining space with zeroes, but if the copied string is greater than or equal the passed buffer length, no null terminator is written, as it wouldn't fit in), so the answer as always is "it depends". Unlike higher-level languages like even Pascal or Java, in C, you don't always get the string structure, here the null terminator, for free.
    – Blauelf
    Aug 25 '17 at 13:00
0

You are printing Ciphertextext: for every character you encrypt. That is, since the print statement with Ciphertextext: is in for loop, it will print Ciphertextext: for every character you encrypt.

To illustrate, consider this,

for(int i = 0; i < 5; i++)
{
    printf("i is: %i", i); 
}

Here output will be,

i is: 0i is: 1i is: 2i is: 3i is: 4 

Did you notice that there is no \n or space after %i in my print statement ?

Contrast that with this,

for(int i = 0; i < 5; i++)
{
    printf("%i", i); 
}

Here output will be,

01234

Again, notice that there is no space or \n after %i in print statement. Hence the next statement is printed on the same line right next to the previous print statement.

I hope this gives you a hint as to where you are going wrong.

You must log in to answer this question.

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