In this caesars cipher code how do i make it so that it reads in spaces, special characters (!##$*...), and numbers and keeps them as they are and prints them? Also i put an fprintf(file2,"\n") at the end of the fgets while loop and it does start a new line but it always prints a weird character at the end of the previous line.
#include <stdio.h>
int main (int argc, char *argv[]) {
char caesar[256];
int shift;
FILE *file1=fopen(argv[1],"r");
FILE *file2=fopen(argv[2],"w");
sscanf(argv[3], "%d", &shift);
while(fgets(caesar,sizeof(caesar),file1)!=NULL){
int i = 0;
while (caesar[i] != '\0') {
if (((caesar[i] + shift) >= 65 && (caesar[i] + shift) <= 90)||((caesar[i] + shift) >= 97 && (caesar[i] + shift) <= 122)) {
caesar[i] += (shift);
}
else {
caesar[i] += (shift - 26);
}
i++;
}
fprintf(file2,"%s", caesar);
fprintf(file2,"\n");
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
int main (int argc, char *argv[]) {
char caesar[256];
int shift;
FILE *file1=fopen(argv[1],"r");
FILE *file2=fopen(argv[2],"w");
sscanf(argv[3], "%d", &shift);
while(fgets(caesar,sizeof(caesar), file1)!=NULL){
char ch;
int i = 0;
for(i = 0; (ch=caesar[i]) != '\0'; ++i) {
if (isalpha(ch)) {//Only alphabet
char a, z;
if(isupper(ch)){
a = 'A'; z = 'Z';
} else {
a = 'a'; z = 'z';
}
caesar[i] += shift;//this depend on the sequence of character codes.
if(caesar[i] > z){
caesar[i] = a + caesar[i] - z - 1;
}
}
}
fprintf(file2, "%s", caesar);
}
return 0;
}
Related
The user inputs chars that can be uppercase or lowercase. I need to spit an answer back that preserves the case of the inputted char after that char is processed by my code. How do I preserve case when the char was converted to its ASCII value and then to its alphabetical index equivalent.
Here is my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
int counter = 0;
if (argc == 2) {
for (int k = 0; k < strlen(argv[1]); k++) {
if (isdigit(argv[1][k])) {
counter++;
}
}
if (strlen(argv[1]) == counter)
{
string s = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0; i < strlen(s); i++) {
int c = (int) s[i];
if (c >= 97 && c <= 122)
{
printf("%i\n", (((c % 32) - 1 + atoi(argv[1]))) % 26);
}
printf("\n");
if (c >= 65 && c <= 90)
{
printf("%i", (((c % 32)) - 1 + atoi(argv[1])) % 26);
}
printf("\n");
}
} else {
printf("Usage: ./caesar key\n");
}
}
}
Expected behavior would print Zoo as "App" if Key entered in command line were 1.
C does some spooky things when I put the nonAlphaCount declaration above the for loop. I can't explain why the outputs are different.
For version 1 (int declaration above main method) my input output was:
INPUT: ./Vigenere.exe bacon
Enter plain text: Meet me at the park at eleven am
OUTPUT: Negh zf av huf pcfx bt gzrwep oz
For version 2 ( int declaration above for loop )
INPUT: ./Vigenere.exe bacon
Enter plain text: Meet me at the park at eleven am
OUTPUT: NRQQ M[L \M^^ KQXXZQZ M
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
const int INPUT_LEN = 255;
const int ALPHABET_LEN = 26;
int nonAlphaCount = 0;
int main (int count, char *args[])
{
char plainText[INPUT_LEN];
char *cipherText;
char *keyWord;
if ( count < 2 || count > 2)
{
printf("There is no key");
return 1;
}
strcpy(keyWord, args[1]);
int keyWord_LEN = strlen(keyWord);
printf("Enter plain text: ");
fgets (plainText, INPUT_LEN, stdin);
int strLength = strlen(plainText);
cipherText = malloc(strLength);
printf("%s", plainText);
for (int i = 0; i < strLength; i++ ){
if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
break;
if(isalpha(plainText[i]))
{
// Default lower
int asciiUpperOrLower = 97;
int keyUpperOrLower = 97;
if(isupper(plainText[i]))
asciiUpperOrLower = 65;
if(isupper(keyWord[i % keyWord_LEN]))
keyUpperOrLower = 65;
int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);
cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
}
else{
cipherText[i] = plainText[i];
nonAlphaCount++;
}
}
// Set string terminator.
cipherText[strLength - 1] = '\0' ;
printf("%s", cipherText);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
const int INPUT_LEN = 255;
const int ALPHABET_LEN = 26;
int main (int count, char *args[])
{
char plainText[INPUT_LEN];
char *cipherText;
char *keyWord;
if ( count < 2 || count > 2)
{
printf("There is no key");
return 1;
}
strcpy(keyWord, args[1]);
int keyWord_LEN = strlen(keyWord);
printf("Enter plain text: ");
fgets (plainText, INPUT_LEN, stdin);
int strLength = strlen(plainText);
cipherText = malloc(strLength);
printf("%s", plainText);
**int nonAlphaCount = 0;**
for (int i = 0; i < strLength; i++ ){
if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
break;
if(isalpha(plainText[i]))
{
// Default lower
int asciiUpperOrLower = 97;
int keyUpperOrLower = 97;
if(isupper(plainText[i]))
asciiUpperOrLower = 65;
if(isupper(keyWord[i % keyWord_LEN]))
keyUpperOrLower = 65;
int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);
cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
}
else{
cipherText[i] = plainText[i];
nonAlphaCount++;
}
}
// Set string terminator.
cipherText[strLength - 1] = '\0' ;
printf("%s", cipherText);
return 0;
}
Both programs exhibit undefined behaviour in
char *keyWord;
...
strcpy(keyWord, args[1]);
which is revealed by the compiler warning: "uninitialized local variable 'keyWord' used". You have not allocated any memory.
If one of the programs happened to work, so be it.
I've been going round in circles with this now for a few hours. It manages the first word of the recommended test (Meet me at the park at eleven am) gets over the first spaces, gives a correct letter for m then prints several spaces before ending. Many thanks in advance.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int allstralpha();
int main(int argc, string argv[])
{
string keyw = argv[1];
if(argc == 2 && allstralpha(keyw))
{
string plaint = GetString();
int c = 0;
int kl = strlen(keyw);
int k = 0;
int p = 0;
int j = 0;
for(int i = 0, n = strlen(plaint); i < n; i++)
{
if(isalpha(plaint[i]))
{
if(isupper(keyw[j]))
{
k = keyw[(j % kl)] - 65;
if(isupper(plaint[i]))
{
p = plaint[i] -65;
c = ((k + p) % 26) + 65;
printf("%c", (char) c);
}
else if(islower(plaint[i]))
{
p = plaint[i] -97;
c = ((k + p) % 26) + 97;
printf("%c", (char) c);
}
}
else if(islower(keyw[j]))
{
k = keyw[(j % kl)] - 97;
if(isupper(plaint[i]))
{
p = plaint[i] - 65;
c = ((k + p) % 26) + 65;
printf("%c", (char) c);
}
else if(islower(plaint[i]))
{
p = plaint[i] - 97;
c = ((k + p) % 26) + 97;
printf("%c", (char) c);
}
}
j++;
}
else
{
printf("%c", (char) plaint[i]);
}
}
}
else
{
printf("Sorry that is not a vaild parameter\n");
return 1;
}
}
int allstralpha(string s)
{
for(int i = 0, n = strlen(s); i < n; i++)
{
if(!isalpha(s[i]))
{
return 0;
}
}
return 1;
}
int allstralpha();
int allstralpha(string s)
{
...
}
Your function definition and declaration don't match. You should declare int allstralpha(string s);
In first line of main:
int main(int argc, string argv[])
{
string keyw = argv[1];
...
}
First you should check if (argc > 1) before accessing argv[1]
For the actual code itself, you provide the plain text, but I can't see the keyword.
I use these values from wikipedia, vigenère cipher for testing:
Plaintext: ATTACKATDAWN
Key: LEMONLEMONLE
Ciphertext: LXFOPVEFRNHR
Minimum code to finish this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, const char* argv[])
{
const char *str = "Meet me at the park at eleven am";
const char *key = "bacon";
int keylen = strlen(key);
int len = strlen(str);
for (int i = 0, j = 0; i < len; i++)
{
int c = str[i];
if (isalnum(c))
{
//int k = function of key and `j`...
//offset k...
if (islower(c))
{
c = (c - 'a' + k) % 26 + 'a';
}
else
{
c = (c - 'A' + k) % 26 + 'A';
}
j++;
}
putchar(c);
}
putchar('\n');
return 0;
}
I'm changing a program from normal array notation to pure pointer notation and I can't receive user input using getchar() in a while loop. I printed out was the program was receiving and it output upside down question marks. I wasn't sure why this was happening because I never changed the variable type. The problem is in the second function to receive user input. Thank you for the help.
#include <stdio.h>
#include <stdlib.h>
/* Function prototypes */
void fillS1(char * x);
void fillS2(char * x, char * y, char z);
void strFilter(char * a, char * b, char c);
int main(int argc, const char * argv[])
{
char s1[42];
char s2[22];
char x = 0;
fillS2(s2, s1, x);
return 0;
}
/* Function to generate a random string of 40 uppercase letters */
void fillS1(char randomlyGeneratedString[])
{
char * pointerToRandom = randomlyGeneratedString;
int i;
for (i = 0; i < 40; i++) {
*(pointerToRandom + i) = 'A' + rand() % 26;
}
pointerToRandom[40] = (char)0;
//printf("This is the generated string %s\n", pointerToRandom);
}
/* Function to get user input of characters */
void fillS2(char userString[], char randomString[], char replacementCharacter)
{
char * pointerToUserString = userString;
char * pointerToRandom = randomString;
int i = 0;
int n = 0;
int capitalLetterCheck = 0;
char loopContinue = 0;
char copyString[42];
char * pointerToCopyString = copyString;
fillS1(pointerToRandom);
do {
/* For loop to copy the first randomly generated string */
for(i = 0; i < 42; i++)
*(pointerToCopyString + i) = *(pointerToRandom + i);
i = 0;
capitalLetterCheck = 0;
/* While loop to to get user input */
printf("Please enter at least 2 capital letters and a maximum of 20.\n");
while (((*(pointerToUserString + i)) = getchar() != '\n')) {
/* Counter to determine how many characters were entered */
i++;
}
i++;
*(pointerToUserString + i) = '\0';
//printf("This is the value if i %i", i);
//printf("This is the user's string %s", pointerToUserString);
/* Capital letter check */
for (n = 0; n < 20; n++) {
if (((*(pointerToUserString + i)) >= 'A') && (*(pointerToUserString + i) <= 'Z'))
{
capitalLetterCheck++;
}
}
if (i < 3) {
printf("You need at least two letters\n");
}
else if (i > 21){
printf("You cannot have more than twenty letters\n");
}
else if (capitalLetterCheck >= 2) {
puts(pointerToUserString);
printf("Enter a character to replace occuring letters.\n");
scanf("%c", &replacementCharacter);
getchar();
strFilter(pointerToCopyString, pointerToUserString, replacementCharacter);
}
else
printf("You must have 2 capital letters.\n");
printf("Would you like to enter another string (y/n)?\n");
loopContinue = getchar();
getchar();
} while (loopContinue != 'n' && loopContinue != 'N');
}
/* Function to replace letters with the character chosen by the user */
void strFilter(char a[], char b[], char c)
{
char * pointerToA = a;
char * pointerToB = b;
int i = 0;
int n = 0;
while (n < 20) {
for (i = 0; i < 40; i++) {
if ((*(pointerToA + i)) == *(pointerToB + n)){
*(pointerToA + i) = c;
}
}
i = 0;
n++;
}
puts(a);
}
The problem is this in the if statement:
(*(pointerToUserString + i)) = getchar() != '\n'
Assignment is an expression, with lower precedence than comparison. This means the above is the same as:
(*(pointerToUserString + i)) = (getchar() != '\n')
So (*(pointerToUserString + i)) gets assigned the value of the expression getchar() != '\n' which is not what you want.
The cipher code actually works; it's just that I get some odd three digit codes separated with slashes too.
Any help would be greatly appreciated. Here's my code.
The codes look like this but have random numbers /354/233/645/810/236
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cs50.h"
int i, len;
string sentance, encrypted;
int k, argvLen;
int caesar (int k){
printf("Hi I'm Ceaser! What would you like me to cipher?\n");
sentance = GetString();
len = strlen(sentance);
char encrypted[len];
for (i=0; i<len; i++) {
if (sentance[i] >='a' && sentance[i] <='z') {
encrypted[i] = ((sentance[i] - 'a' + k) % 26) + 'a';
}
else if (sentance[i] >='A' && sentance[i] <='Z') {
encrypted[i] = ((sentance[i] - 'A' + k) % 26) + 'A';
}
else if (sentance[i] >=' ' && sentance[i] <= '#'){
encrypted[i] = sentance[i];
}
}
printf("%s", encrypted);
return 0;
};
int main (int argc, const char * argv[]) {
if (argc==2) {
k = atoi(argv[1]);
argvLen = strlen(argv[1]);
for (i=0; i<argvLen; i++){
if (isdigit(argv[1][i])){
caesar(k);
}
else {
printf("please enter a number for the key!");
return 1;
}
}
return 0;
}
};
You are not terminating the encrypted string properly.
You need:
To make sure you have room for the terminating character, by using char encrypted[len + 1];.
encrypted[len] = '\0'; after the loop.