On this week Problem set I had to make a text cipher solution made with C, this was a great challenge and it was very fun because you get used to working with arrays and ASCII values test a few solutions before finding the right solution to this problem.
If you are starting to Program with C and you are researching for a solution for this problem here I'm sharing how I made my solution that passed the CS50 check command.
Before starting remember that:
- Every letter has an ASCII value
- This is a great example to have a look before starting to code your solution.
First we start with a main function that it will take 2 arguments, first one takes number of arguments, and the second one takes an array of strings, like so.
int main(int argc, string argv[])
Then, we need to checked if the user inputs less than or more than two arguments the user should be re-prompt. The second check we need to do if one of the arguments that the user inputs is alpha if it not alpha the user needs to e re-prompt again, we are going to loop through the string and we are going to check if the argument isalpha.
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./ceasar k");
return 1;
}
// checking if one of the arguments isalpha (we need an integer) - Looping through the string of integers
for (int key = 0; key < strlen(argv[1]); key++)
{
if (isalpha(argv[1][key]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
Now, we are going to use a function called atoi, atoi it takes a string and transform that string into an integer. Then, we ask the user to input the plaintext and we need to loop through each letter of the plaintext, if it's not alpha we are going to print the current element of the array.
// iterates over the plain text with a for loop
for (int i = 0, length = strlen(plaintext); i < length; i++)
{
if (!isalpha(plaintext[i]))
{
//prints the current element of the array if it's not alpha
printf("%c", plaintext[i]);
continue;
}
Then, we are going to check if the current letter is uppercase and we calculate how far the current element is from lowercase "a" or uppercase "A".
Finally, we apply the formula that it was giving to us so we can know exactly the index of the ciphering of the letter that we want to cipher and we print the final result
// index of the letter cyphering
int ci = (pi + key) % 26;
// printing the new character cyphered
printf("%c", ci + offset);
}
printf("\n");
return 0;
I hope this is helpful and as always I'm leaving the whole solution down below if you want to have a look. Keep coding, Keep learning!
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// main function takes 2 arguments, first one takes number of arguments, and the second one takes an array of strings
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./ceasar k");
return 1;
}
// checking if one of the arguments isalpha (we need an integer) - Looping through the string of integers
for (int key = 0; key < strlen(argv[1]); key++)
{
if (isalpha(argv[1][key]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]) % 26; // converts the ASCII to an integer from "20" to 20 as an interger
// takes the plaintext from the user
string plaintext = get_string("plaintext: ");
printf("ciphertext: ");
// iterates over the plain text with a for loop
for (int i = 0, length = strlen(plaintext); i < length; i++)
{
if (!isalpha(plaintext[i]))
{
//prints the current element of the array if it's not alpha
printf("%c", plaintext[i]);
continue;
}
// checking if the current element it's uppercase
int offset = isupper(plaintext[i]) ? 65 : 97;
// calculating how far the current element is from lowercase "a" or uppercase "A"
int pi = plaintext[i] - offset;
// index of the letter cyphering
int ci = (pi + key) % 26;
// printing the new character cyphered
printf("%c", ci + offset);
}
printf("\n");
return 0;
}
Follow me on Github & Connect with me on LinkedIn
https://github.com/cesareuseche
https://www.linkedin.com/in/cesar-useche-profile/
Top comments (4)
dude i literarily only signed up just so i could than you for explaining this so well,with this ill be able to write the code without needing to copy yours at all
That's awesome, I'm glad I was able to help you!
Awesome use of that ternary!
Thank you! :)