#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(int count, string input[])
{
// array of small alphabets
char smaAlph[26] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
// capital aphabets
char capAlph[26] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
int key;
if (count == 2)
{
int digit, n;
// assigning the string length
n = strlen(input[1]);
for (int i = 0; i < n; i++)
{
if (input[1][i] < '0' || input[1][i] > '9')
{
printf("Usage: ./caesar key\n");
return 1;
}
}
// getting the key
key = atoi(input[1]);
// trying encryption
//getting normal text
string text = get_string("plaintext: ");
int n2 = strlen(text);
for (int i = 0; i < n2; i++)
{
// testing if the char alphabet
if (isalpha(text[i]))
{
// testing if the char upper text
if (isupper(text[i]))
{
// looping through the cap alphabet array
for (int p = 0; p < 26; p++)
{
// testing if the char matches the char in the array
// switcing from ASCII index to alphabet index
if (capAlph[p] == text[i])
{
// applying the formula & returning to ascii
text[i] = ((capAlph[p] + key) % 26) + 'A';
}
}
}
// testing if the char lower text
if (islower(text[i]))
{
for (int p = 0; p < 26; p++)
{
// looping through the cap alphabet array
// doing same as the CAP alphabet
if (smaAlph[p] == text[i])
{
// switching to smaAlph index and ascii
text[i] = ((smaAlph[p] + key) % 26) + 'a';
}
}
}
}
}
printf("ciphertext: %s\n", text);
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 2;
}
}
my code is compiling correctly but it doesn't function correctly (doesn't print the expected output) That is required in the assessment. It should cypher the input text by shifting the input alphabets (and only alphabets) by the value specified in the key input.
Edit
#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(int count, string input[])
{
// array of small alphabets
char smaAlph[26] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
// capital aphabets
char capAlph[26] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
int key;
if (count == 2)
{
int digit, n;
// assigning the string length
n = strlen(input[1]);
for (int i = 0; i < n; i++)
{
if (input[1][i] < '0' || input[1][i] > '9')
{
printf("Usage: ./caesar key\n");
return 1;
}
}
// getting the key
key = atoi(input[1]);
// trying encryption
//getting normal text
string text = get_string("plaintext: ");
int n2 = strlen(text);
for (int i = 0; i < n2; i++)
{
// testing if the char alphabet
if (isalpha(text[i]))
{
// testing if the char upper text
if (isupper(text[i]))
{
// looping through the cap alphabet array
for (int p = 0; p < 26; p++)
{
// testing if the char matches the char in the array
// switcing from ASCII index to alphabet index
if (capAlph[p] == text[i])
{
// applying the formula & returning to ascii
text[i] = ((capAlph[p] + key) % 26);
// returning to ascii
text[i] += 'A';
}
}
}
// testing if the char lower text
if (islower(text[i]))
{
for (int p = 0; p < 26; p++)
{
// looping through the cap alphabet array
// doing same as the CAP alphabet
if (smaAlph[p] == text[i])
{
// switching to smaAlph index and ascii
text[i] = ((smaAlph[p] + key) % 26);
// returning to ascii
text[i] += 'a';
}
}
}
}
}
printf("ciphertext: %s\n", text);
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 2;
}
}