Skip to main content
code removed per academic honesty policy
Source Link
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43
// program encrypts messages using Caesar’s cipher
 
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

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

    // program accept a single command-line argument
    // a non-negative integer corresponding to the key

    if (argc != 2)
    {
        printf("Usage : ./asciimath key\n");
        return 1;
    }

    int key = atoi(argv[1]);

    // program prompt the user for a string of plaintext

    string plaintext = GetString();

    // length : lenght of the string user provides
    // i, j : incrementation variables
    // Lcase, Ucase : first character of the alphabet in lower and uppercase
    // alpha, ALPHA : strings of characters corresponding to the alphabet
    // in lower and uppercases.

    int length = strlen(plaintext), i = 0, j = 0, Lcase = 'a', Ucase = 'A';
    char alpha[26], ALPHA[26], cipher[length];

    for ( ; j <= 25; j++)
    {
        alpha[j] = Lcase;
        ALPHA[j] = Ucase;
        Lcase++;
        Ucase++;
    }
       
    while (i < length)
    {
        // character validation
        if (isalpha(plaintext[i]))
        {
    
            // upper or lower cases validation
            if (isupper(plaintext[i]))
            {
                cipher[i] = ALPHA[(( (int) plaintext[i] - 65) + key) % 26];
                i++;
            }    
        
            if (islower(plaintext[i]))
            {
                cipher[i] = alpha[(( (int) plaintext[i] - 97) + key) % 26];
                i++;
            }
        }
            
        else
        {
            cipher[i] = plaintext[i];
            i++;
        }
    }
 
    printf("%s\n", cipher);
    
    return 0;

}

[code removed]

// program encrypts messages using Caesar’s cipher
 
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

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

    // program accept a single command-line argument
    // a non-negative integer corresponding to the key

    if (argc != 2)
    {
        printf("Usage : ./asciimath key\n");
        return 1;
    }

    int key = atoi(argv[1]);

    // program prompt the user for a string of plaintext

    string plaintext = GetString();

    // length : lenght of the string user provides
    // i, j : incrementation variables
    // Lcase, Ucase : first character of the alphabet in lower and uppercase
    // alpha, ALPHA : strings of characters corresponding to the alphabet
    // in lower and uppercases.

    int length = strlen(plaintext), i = 0, j = 0, Lcase = 'a', Ucase = 'A';
    char alpha[26], ALPHA[26], cipher[length];

    for ( ; j <= 25; j++)
    {
        alpha[j] = Lcase;
        ALPHA[j] = Ucase;
        Lcase++;
        Ucase++;
    }
       
    while (i < length)
    {
        // character validation
        if (isalpha(plaintext[i]))
        {
    
            // upper or lower cases validation
            if (isupper(plaintext[i]))
            {
                cipher[i] = ALPHA[(( (int) plaintext[i] - 65) + key) % 26];
                i++;
            }    
        
            if (islower(plaintext[i]))
            {
                cipher[i] = alpha[(( (int) plaintext[i] - 97) + key) % 26];
                i++;
            }
        }
            
        else
        {
            cipher[i] = plaintext[i];
            i++;
        }
    }
 
    printf("%s\n", cipher);
    
    return 0;

}
// program encrypts messages using Caesar’s cipher

[code removed]

Source Link

Here my final program for those who are interested. This is certainly not the finest way to write it but I'll stand this for now. Thanks for help.

// program encrypts messages using Caesar’s cipher

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

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

    // program accept a single command-line argument
    // a non-negative integer corresponding to the key

    if (argc != 2)
    {
        printf("Usage : ./asciimath key\n");
        return 1;
    }

    int key = atoi(argv[1]);

    // program prompt the user for a string of plaintext

    string plaintext = GetString();

    // length : lenght of the string user provides
    // i, j : incrementation variables
    // Lcase, Ucase : first character of the alphabet in lower and uppercase
    // alpha, ALPHA : strings of characters corresponding to the alphabet
    // in lower and uppercases.

    int length = strlen(plaintext), i = 0, j = 0, Lcase = 'a', Ucase = 'A';
    char alpha[26], ALPHA[26], cipher[length];

    for ( ; j <= 25; j++)
    {
        alpha[j] = Lcase;
        ALPHA[j] = Ucase;
        Lcase++;
        Ucase++;
    }
       
    while (i < length)
    {
        // character validation
        if (isalpha(plaintext[i]))
        {
    
            // upper or lower cases validation
            if (isupper(plaintext[i]))
            {
                cipher[i] = ALPHA[(( (int) plaintext[i] - 65) + key) % 26];
                i++;
            }    
        
            if (islower(plaintext[i]))
            {
                cipher[i] = alpha[(( (int) plaintext[i] - 97) + key) % 26];
                i++;
            }
        }
            
        else
        {
            cipher[i] = plaintext[i];
            i++;
        }
    }
 
    printf("%s\n", cipher);
    
    return 0;

}