0

My code seems to be working but when I enter only one or few characters in my plaintext, my cyphertext returns weird characters... I had a look at my code and try couple of solutions and I can see that the issue is fixed when I remove the usage of strlen. Based on reading on internet, I understood that strlen could have brought rubbish data in my array but I can't find a solution to clean my arrays or to make strlen work better.

for example:

plaintext: a
ciphertext: b���

Thank you for any advice!

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

int main(int argc, string argv[])
{
    string key = argv[1];

    //check if there is only one argument
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    } 

    //check if the string has only digit
    for (int i = 0 ; i < strlen(key) ; i++)
    {
        if (key[i] < '0' || key[i] > '9')
        {
            printf("Usage: ./caesar key\n");
            return 1;  
        } 
    }
    int k = atoi(argv[1]);
    printf("%i\n", k);

    //Prompt for Plain text
    string p = get_string("plaintext: ");
    int lenp = strlen(p);

    //Create ciphertext
    char c[lenp];

    for (int i = 0; i < lenp; i++)
    {        
        int pn = (int) p[i];

        //no change if character is not a letter
        if (pn < 65 || pn > 122 || (pn > 90 && pn < 97))
        {
            c[i] = p[i];
        }
        // to keep uppercase
        else if (pn >= 65 && pn <= 90)
        {
            pn = pn - 65;
            int cn = ((pn + k) % 26) + 65 ;
            c[i] = (char) cn;
        }

        //to keep lowercase
        else 
        {
            pn = pn - 97;
            int cn = ((pn + k) % 26) + 97 ;
            c[i] = (char) cn;
        }
    }
    printf("ciphertext: %s\n", c);
    return 0;
}
0

Hmmm.....

When you created the char array c, did you leave room for, and add an end of string marker?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Hi Cliff, thank you for your answer. I don't think really know how to answer your question. I have uploaded my code above. I did try to add a '/0' value for my array in position [i+1] but it didn't work. Nov 12 '19 at 14:22
  • Found it, thanks to another comment on Discord. The solution Instead of c[lenp] do c[lenp + 1] And make the last character a '\0'. It works perfectly fine Nov 12 '19 at 17:15
0

i had the same issue originally.

in your line char c[lenp];

you need to leave room for the \0 in your array since lenp is just reading the legnth of p as read by a human without factoring in the \0. so by replacing lenp with lenp+1 your outputs shouldn't show those odd looking characters hope it helps and good luck!

1
  • this comment just saved my sanity. Been struggling with this "funny character business" for days. Thanks!
    – nwbee
    Mar 27 '20 at 18:27

You must log in to answer this question.

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