Jump to content

Delete this before posting. Please make sure to include the language that you're using in the title, and use the <> button for any code.

Going through the cs50x course. Just asking for general tips. Thinking I did alright, but it's definitely a lot harder than python.

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

string caesar(string p, string q, int key);
bool all_digits(string s);
int keyint(string o, int initial);

int main(int argc, string argv[])
{
    if (argc != 2 || all_digits(argv[1]) != true)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    int sum = 0;

    int keynum = keyint(argv[1], sum);

    string original = get_string("plaintext:  ");

    char coded[strlen(original)];

    string changed = caesar(original, coded, keynum);

    printf("ciphertext: %s\n", changed);
}

bool all_digits(string s)
{
    for (int i = 0, n = strlen(s); i < n; i++)
    {
        if (s[i] < '0' || s[i] > '9')
        {
            return false;
        }
    }
    return true;
}

string caesar(string p, string q, int key)
{
    for (int i = 0, n = strlen(p); i < n; i++)
    {
        if (p[i] >= 'A' && p[i] <= 'Z')
        {
            q[i] = (char) (((p[i] - 'A') + key) % 26) + 'A';
        }
        else if (p[i] >= 'a' && p[i] <= 'z')
        {
            q[i] = (char) (((p[i] - 'a') + key) % 26) + 'a';
        }
        else
        {
            q[i] = p[i];
        }
    }
    q[strlen(p)] = '\0';
    return q;
}

int keyint(string o, int initial)
{
    for (int i = 0; o[i] != '\0'; i++)
    {
        initial = initial * 10 + (o[i] - 48);
    }
    return initial;
}

 

Link to comment
https://linustechtips.com/topic/1583778-caesars-cipher-c/
Share on other sites

Link to post
Share on other sites

  • 1 month later...
11 hours ago, mazylol said:

Why did you declare the functions above main? Very weird to do it that way. Also not a huge fan of the Allman brace style, personally.

 

On 9/26/2024 at 9:44 PM, okkee said:
string caesar(string p, string q, int key);
bool all_digits(string s);
int keyint(string o, int initial)

I'm guessing you mean this part here. I just kinda thought you had to put the prototype above main. Like, so that main doesn't think it's going to call a function that doesn't exist.

Link to comment
https://linustechtips.com/topic/1583778-caesars-cipher-c/#findComment-16571179
Share on other sites

Link to post
Share on other sites

6 hours ago, okkee said:

 

I'm guessing you mean this part here. I just kinda thought you had to put the prototype above main. Like, so that main doesn't think it's going to call a function that doesn't exist.

Typically you would just put main at the bottom of the program so you do not have to declare the functions like that. There is absolutely nothing wrong with doing it that way though.

Link to comment
https://linustechtips.com/topic/1583778-caesars-cipher-c/#findComment-16571405
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×