0

I know it sounds real stupid, but It is cuz I just lightly learned Java and Python last year...

now I am try to do C properly with CS50.

on week 2, there is Caesar encipher text program task.

I tried to use += on string to stack char(s) one by one.

And of course it didn't work.

but is there any other way to do it similarly?

here is the code

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


string encipher(string x, int y);


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

    for (int i = 0; i < strlen(key); i++)
    {
        int k = isdigit(key[i]);
        if (k == 0)
        {
            num_flag = true;
        }

    }

    if (num_flag == true)
    {
        printf("Usage: ./caesar key\n");
    }
    else
    {
    int keys = atoi(argv[1]);

    string c = get_string("plaintext: ");
    string ciphertext = encipher(c, keys);
    printf("ciphertext: %s\n", ciphertext);
    }
}


string encipher(string x, int y)
{
    string cip = "";
    for (int i = 0; i < strlen(x); i++)
    {
        int low = islower(x[i]);
        int upper = isupper(x[i]);

        if (low != 0)
        {
            char lo = x[i] - 97;
            char c = (lo + y) % 26;
            char l = c + 97;
            cip += l;
        }
        else if (upper != 0)
        {
            char up = x[i] - 65;
            char c = (up + y) % 26;
            char u = c + 65;
            cip += u;
        }
        else
        {
            cip += x[i];
        }
    }
    return cip;
}

.

I changed code to pass the test, but not perfect either...

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


string encipher(string x, int y);


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



    if(argc != 2)
    {
        printf("Usage: ./caesar key\n");
    }
    else
    {
        string key = argv[1];
        bool num_flag = false;

        for (int i = 0; i < strlen(key); i++)
        {
            int k = isdigit(key[i]);
            if (k == 0)
            {
                num_flag = true;
            }
        }

        if (num_flag == true)
        {
            printf("Usage: ./caesar key\n");
        }
        else
        {
            int keys = atoi(argv[1]);

            string c = get_string("plaintext: ");
            string cipher = encipher(c, keys);
            printf("ciphertext: %s\n", cipher);
        }
    }
}


string encipher(string x, int y)
{

    for (int i = 0; i < strlen(x); i++)
    {
        int low = islower(x[i]);
        int upper = isupper(x[i]);

        if (low != 0)
        {
            char lo = x[i] - 97;
            char c = (lo + y) % 26;
            char l = c + 97;
            x[i] = l;
        }
        else if (upper != 0)
        {
            char up = x[i] - 65;
            char c = (up + y) % 26;
            char u = c + 65;
            x[i] = u;
        }
    }
    return x;
}

Thanks for helping!

0

That is probably the best solution to the problem. You cannot append to a string, because as you learn later in week 4, a certain space of memory is reserved for a string depending on the length. If you attempt to make the string longer and take up more space, it might meddle with other data that comes after the string and create errors.

You must log in to answer this question.

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