fgets() creating another copy of string at time of input? - c

I'm wondering what the error is here? I'm making a causer cipher, and it works fine but when I print the encrypted message at the end, it reprints the unencrypted message. When I comment out and set message to a sentence, it works fine. But when I get a string using fgets it creates another copy. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
char message[100];
printf("Please Enter a message to be Encrypted: ");
fgets(message, 100, stdin);
unsigned char caeser[] = "";
int j = 0;
if(argc >= 2)
{
int k = atoi(argv[1]);
for(int i = 0, n = strlen(message); i < n; i++)
{
printf("%i\n",n);
if(!isspace(message[i]))
{
if(islower(message[i]))
{
j = message[i] - 97;
caeser[i] = 97 + ((j + k) % 26);
}
if(isupper(message[i]))
{
j = message[i] - 65;
caeser[i] = 65 + ((j + k) % 26);
}
}
else
{
caeser[i] = ' ';
}
}
printf("%s\n", caeser);
}
}

There is no issue with fgets() here. The real issue here is you did not define the array caeser properly.
You need to provide the size explicitly if you don't initialize with enough initializers at definition time. A compile-time array won't magically expand based on the usage. You have to pre-decide the size, either by mentioning it explicitly or by initializing it with enough number of initalizer elements.
Here, something like
unsigned char caeser[100] = {0};
would do the job for you.

Related

Problem using C. Program works then acts differently for unknown reason

So in my program I retrieve a command line argument (must be 26 characters with no duplicates) which is used kind of like a rubric for a cypher or something and all these letters are put into an array (i know im not doing it super efficiently).
Following this I prompt for a user to write something and that sentence will in turn change based on what the CLA is inputted as a "cypher" i guess. When i do do this and the cypher is simply just the alphabet (a-z) [therefore should returning the exact same thing written in the prompt] the first couple letters are correct and follow the logic of my code however after getting to the 5th it starts to print out strange random letters for unknown reasons.
ex. hi there how's it going = hi thhrh how's it roisr
plss help :D
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char letters[] = {};
char word[] = {};
// Takes input "Code" and checks if it is suitable (so far) still need to input reminder if no key etc.
int main(int argc, string argv[])
{
if (argc !=2)
{
printf("Missing command-line argument\n");
return 1;
}
else if ((argv[1][1]) == ' ')
{
printf("Usage: ./substitution key");
return 1;
}
else if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isalpha(argv[1][i]) != 0)
{
letters[i] = argv[1][i];
}
else
{
printf("Key must only contain alphabetic characters.\n");
return 1;
}
for (int j = 0; j < i; j++)
{
if (toupper(argv[1][j]) == toupper(argv[1][i]))
{
printf("No Repeat Characters\n");
return 1;
}
}
// confirmed this prints the entire focking CLA printf("%c", letters[i]);
}
string ptext = get_string("plaintext: ");
printf("cyphertext: ");
for (int j = 0; j < strlen(ptext); j++)
{
if (ptext[j] >= 'A' && ptext[j] <= 'Z')
{
int l = ptext[j] - 65;
char z = letters[l];
//printf("%c\n", z);
word[j] = z;
printf("%c", word[j]);
}
else if (ptext[j] >= 'a' && ptext[j] <= 'z')
{
int k = ptext[j] - 97;
char y = letters[k];
word[j] = y;
printf("%c", word[j]);
}
else
{
printf("%c", ptext[j]);
}
}
printf("\n");
}
thats the code!
I've tried debugging and looking into why the value changes however it just suddenly makes letters[k] not equal to e when it should as it is in the array made earlier in the code. I'm not sure what's happening as im pretty sure the code has sound logic
here you have created zero size arrays
char letters[] = {};
char word[] = {};
you need to make them larger. I guess you need
char letters[26] = {};
char word[50] = {};
not quite sure what max size you need tho

C - Cycle through all possible lowercase strings

I'm learning C with the CS50 course problem set 2, using the crypt function to brute force guess a password. Currently writing a function that prints all possible strings of a certain length, eg:
aa
ab
...
az
ba
...
zy
zz
I've written a fairly simple recursive function to do so:
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>
void stringcycler(int n, int passLength, char *pass)
// Scrolls through all lowercase letter combinations for a string of length passLength
// Expects an integer value of the length of the strng as both n and passLength
// Also expects a char* array of length passLength with all chars set to 'a' (and a null character)
{
if(n != 0)
{
for(pass[passLength - n] = 'a'; pass[passLength - n] < 'z'; pass[passLength - n]++)
{
stringcycler(n-1, passLength, pass);
printf("%s\n", pass);
// return 0;
}
}
}
int main()
{
// Initialise char *c, and scroll through letters
int passLength = 2; // The number of characters you want to brute force guess
char pass[passLength + 1]; // Add 1 for the null character
int i;
for(i = 0; i < passLength; i++) pass[i] = 'a'; // Set every char in pass to 'a'
pass[passLength] = '\0'; // Set null character at the end of string
stringcycler(passLength, passLength, pass);
return 0;
}
It works for the most part, but only goes to yz. Whenever it sees a z it basically skips, so it goes to yz, then never does za to zz. If I add an = to the for loop line:
pass[passLength - n] < 'z';
ie.
pass[passLength - n] <= 'z';
Then it prints '{' characters in the mix. Any help? And another question is, how can I change this to work for all combos of upper and lower case too, is there a neat way of doing it?
You print after you return from you recursion, but you should print when the recursion has reached the end (or beginning, in your case) of the string. In other words, printing should be an alternative branch to recursing:
void stringcycler(int n, int len, char *pass)
{
if (n != 0) {
for (pass[len - n] = 'a'; pass[len - n] <= 'z'; pass[len - n]++) {
stringcycler(n - 1, len, pass);
}
} else {
printf("%s ", pass);
}
}
The if part constructs the strings as it recurses further down. The else part does something with the constructed string. (Of course, you must include 'z' in your loop. Your original code only prints the z in the last place, because it prints after ther recursion returns, which means thet the char buffer is in a condition that wouldn't (re-)enter the loop.)
Below is a generic backtracking algorithm for generating the password. The idea here is to imagine filling the slots for a given char array a. We will be generating the possible candidates for the given position k for the array a. I have taken the candidates as lower case ascii letters a-z and upper case ASCII letters A-Z. If you want to include other ASCII characters, just modify the construct_candidates function accordingly.
Once the array is filled i.e. k becomes PASS_LEN, we know we have generated the password, we can process it however we like, I have just printed the password here.
The value of PASS_LEN macro can be adjusted to generate password of any desired length.
#include <stdio.h>
#include <stdlib.h>
#define PASS_LEN 2
static char* construct_candidates (char a[], int k, int *count)
{
/* Lower case ASCII */
int min1 = 97;
int max1 = 122;
/* Upper case ASCII */
int min2 = 65;
int max2 = 90;
*count = (max1 - min1 + 1) + (max2 - min2 + 1);
char *cand = calloc(*count, sizeof(char));
if (cand == NULL) {
printf("malloc failed\n");
return NULL;
}
int idx = 0;
for (int i = min1; i <= max1; i++) {
cand[idx] = i;
idx++;
}
for (int i = min2; i <= max2; i++) {
cand[idx] = i;
idx++;
}
return cand;
}
static void backtrack(char a[], int k)
{
int i;
if (k == PASS_LEN) {
for (i = 0; i < PASS_LEN; i++) {
printf("%c", a[i]);
}
printf("\n");
return;
}
int cand_count = 0;
char *cand = construct_candidates(a, k, &cand_count);
if (cand == NULL) {
printf("Failed to get candidates\n");
return;
}
for (i = 0; i < cand_count; i++) {
a[k] = cand[i];
backtrack(a, k + 1);
}
free(cand);
}
int main()
{
char a[PASS_LEN] = {'\0'};
backtrack(a, 0);
}

Struggling with Vigenere! (In C) invalid operands to binary expression ('int *' and 'int') and other things

we are now working with caesar, vigenere. I mannaged to finish caesar but vigenere is not working as good. C gives me back: invalid operands to binary expression ('int *' and 'int'). I'm not sure what the program means exactly and what is wrong with my code. Can somebody help me out or give me advice? I think it's because I can not count with the different types of numbers? I'm not sure!
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("You have to input a key, try again!\n");
return 1;
}
string key = argv[1];
int keylength = strlen(key);
for (int i = 0; i < keylength; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Please insert letters, nothing else\n");
return 1;
}
}
printf("plaintext: ");
string plain = get_string();
int keycipher[keylength];
for(int i = 0; i < keylength; i++)
{
keycipher[i] = toupper(key[i]) - 65;
}
if (plain != 0)
{
printf("ciphertext: ");
int i;
for (i = 0, keylength = strlen(key); i < keylength; i++)
{
if (isupper(plain[i]))
{
printf("%c", (plain[i] - 65 + keycipher) % 26);
}
else if (islower(plain[i]))
{
printf("%c", (plain[i] - 97 + keycipher) % 26);
}
else if (plain[i] == ' ')
{
printf(" ");
}
else
{
printf("%c", plain[i]);
}
}
}
return 0;
}
As mentioned in the comments, you have to use + keycipher[index] instead of + keycipher. This is because keycipher is an array and you need to use only one of the elements of the array at a time.
Further, you will need 2 counter variables instead of just one in the part where you find the ciphertext. This is because you need to increment the plaintext index and the key index in every iteration, but once the plaintext length crosses the key length, you will need to reset the key index.
If you check the specifications, you will understand what I am saying about using 2 counters.

Vigenere Cipher. Code output

I've been working on cs50 pset2, and I thought I had the vigenere cipher down after working on it for a few days. This code is meant to take an alphabetical argument(argv[]) given by the user, and use that as a key to crypt a phrase given by the user(string) by its number in the alphabetical index. For example, if you give the argument 'abc' and the string 'cat' then the output should be 'cbv'(a moving 0, b moving 1, c moving 2) The argument should also wrap around so that if the string is longer, the argument will wrap to its first character and continue until the string has ended.
This is what I have for code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("Try again\n");
return 1;
}
string k = (argv[1]);
int klen = strlen(k);
for(int x = 0; x < klen; x++)
{
if(isalpha(k[x]))
{
if(isupper(k[x]))
{
k[x] = tolower(k[x]);
}
k[x] -= 'a';
}
else
{
printf("Try again\n");
return 1;
}
}
string code = GetString();
int clen = strlen(code);
for(int a = 0, b = 0; a < clen; a++)
{
if(isalpha(code[a]))
{
int key = k[b%klen];
if(isupper(code[a]))
{
printf("%c", (((code[a] - 'A') + key)%26) + 'A');
b++;
}
else
{
printf("%c", (((code[a] - 'a') + key)%26) + 'a');
b++;
}
}
else
{
printf("%c", code[a]);
}
}
printf("\n");
}
The code seems to work for the length of the key +1.
For example,
I input an argument of 'aaaa'
Then input a string of 'bbbbb'
and receive 'bbbbb' correctly.
However, if I input the same 'aaaa'
Then input a string longer than the key +1 'bbbbbbb'
I receive 'bbbbbNN'
I believe I have an issue with my order of operations but have tried moving parenthesis around to no avail. I was hoping someone could point me in the right direction as to why my key isn't wrapping properly.
Your biggest risk with code like this is all the similar, repetitive clauses. A bug in just one is hard to track done. And doing any processing on the key, while processing the code, is just inefficient.
Here's a rework that completely processes the key before processing the code and tries to get the processing down to just one case. See if it works any better for you:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
fprintf(stderr, "Try again\n");
return EXIT_FAILURE;
}
string key = strdup(argv[1]);
size_t key_length = strlen(key);
for (int x = 0; x < key_length; x++)
{
if (isalpha(key[x]))
{
if (isupper(key[x]))
{
key[x] = tolower(key[x]);
}
key[x] -= 'a';
}
else
{
fprintf(stderr, "Try again\n");
return EXIT_FAILURE;
}
}
string code = GetString();
int code_length = strlen(code);
for (int a = 0, b = 0; a < code_length; a++)
{
if (isalpha(code[a]))
{
int start = isupper(code[a]) ? 'A' : 'a';
printf("%c", (((code[a] - start) + key[b++ % key_length]) % 26) + start);
}
else
{
printf("%c", code[a]);
}
}
printf("\n");
free(key);
return EXIT_SUCCESS;
}

Vigenere cipher in cs50

I'm working my way through an online class teaching me how to code. I'm very new to this and have been slowly making my way through this class. I've run into an issue with the vingenere cipher. It doesn't iterate the key through the whole input.
Edit: the key should iterate through the user input and when it reaches the end of the key, loop back and begin again. The key should also skip over any special character(!##" ",etc)
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main (int argc, string argv[])
{
if(argc !=2)
{
printf("please put in command line argument: example - ./vigenere command\n");
return 1;
}
string key = argv[1];
int keylength = strlen(key);
for (int i=0;i<keylength; i++)
{
if(!isalpha(key[i]))
{
printf("please make sure command is letter only. Please no numbers or special characters!\n");
return 1;
}
}
string input = GetString();
for (int i=0, k=0; i<keylength; i++)
{
if(isalpha(input[i]))
{
if(isupper(input[i]))
{
input[i]=((input[i]-'A')+(key[k%keylength]))%26+'A';
}
else
{
if(islower(input[i]))
{
input[i]=((input[i]-'a')+(key[k%keylength]))%26+'a';
}
}
}
}
printf("%s\n",input);
return 0;
}
I know string is not normal, but it's included in the header to help with new students. I guess we learn more as the class progresses.
You didn't change k in your for loop. And indeed I don't think you need k at all. And your loop only iterate through the length of key instead of the length of input.
int inputlength = strlen(input);
for (int i = 0; i < inputlength; ++i) {
if (isupper(input[i]))
input[i] = ((input[i]-'A') + (key[i%keylength])) % 26 + 'A';
/* ... ^ Use i here */
}
Regarding the issue that when the key is b and input is A, you must adjust the key.
input[i] = ((input[i]-'A') + (key[i%keylength]-'a')) % 26 + 'A';
To skip input special characters,
int inputlength = strlen(input);
for (int i = 0, k = 0; i < inputlength; ++i) {
if (isupper(input[i]))
input[i] = ((input[i]-'A') + (key[(k++)%keylength])) % 26 + 'A';
/* ... ^^^ */
else if (islower(input[i]))
input[i] = ((input[i]-'a') + (key[(k++)%keylength])) % 26 + 'a';
}

Resources