PSET2 Caesar: Ordered Comparison Between Pointer and Integer Error - c

I am currently taking Harvard's CS50 course (Intro to CompSci).
I have not even learned pointers yet, I am confused on this error message. The line it is facing an error on in question is this: For context, the full message is as follows:
caesar.c:48:36: error: ordered comparison between pointer and integer ('string' (aka 'char *') and 'char') [-Werror] if (plaintext[j] + key > z)
So, I am unable to understand the error message due to not yet being educated on pointers. Help50 is useless when I compiled with it. Here is my code if it is needed, I hope the comments help!
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
//Creates string key
string key = argv[1];
//Checks for appropriate argument count
if (argc != 2)
{
printf("Usage: ./caesar key");
return 1;
}
//Checks if all argument characters are digits
for (int i = 0; i < strlen(key); i++)
{
if (isdigit(key[i]) == 0)
{
printf("Usage: ./caesar key");
return 1;
}
}
//Gets plaintext from user
string ciphertext = NULL;
char z;
string plaintext = get_string("plaintext: ");
//Converts plaintext to ciphertext
for (int j = 0; j < strlen(plaintext); j++)
{
//Asks if character is alphabetical
if (isalpha(plaintext[j]))
{
//Asks if character is uppercase and assigning ASCII code accordingly
if (isupper(plaintext[j]))
{
z = 90;
}
else
{
z = 122;
}
//Performs conversion operation
if (plaintext[j] + key > z)
{
ciphertext[j] = plaintext[j] + key - 25;
}
else
{
ciphertext[j] = plaintext[j] + key;
}
}
//Keeps text the same since it is not a letter, and therefore shouldn't be shifted
else
{
ciphertext[j] = plaintext[j];
}
}
printf("ciphertext: %s\n", ciphertext);
}
Thank you!

//Creates string key
string key = argv[1];
key is a "string", not an integer value.
if (plaintext[j] + key > z)
The code is attempting to add a char to a string, then compare that to a char.
You need to go back to the basics and review the lesson until you understand arrays of characters and strings.
string ciphertext = NULL;
Here, you initialise a string, then seem to expect it to somehow grow as you later write characters into that string.
Sit down with a paper and pencil and diagram out what you expect to be happening with memory storage...

Related

Why does this program return a "?" symbol in C

I'm following along with cs50x and in problem set 2. This is the idea I had for solving the Caesar problem. I'm yet to implement the key idea due to the fact that it won't print out the word. I'm new to arrays and have searched a bit about why this is occurring. I think that I'm overcomplicating the code and could just use the string given by the user instead of transferring it to a function but now that I've started the idea I want to know why it isn't working and if there is a way to make it work. When ran, the program should accept a command line of a single number, if it has no command line it should fail, if the number is negative it should fail, if it is not a number it should fail and if it has more than 1 argument it should fail. Thanks
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
string cipher(string word, int key);
int main(int argc, string argv[])
{
// Checks whether the user inputted only 1 argument
if (argc == 2)
{
// Convert argv to an int
int key = atoi(argv[1]);
string plainText = get_string("plaintext: ");
// Use function to return the (soon to be encrypted) string
string cipherText = cipher(plainText, key);
// Print for how long the word is
int n = strlen(plainText);
for (int i = 0; i < n; i++)
{
// Print the char of the array based upon the iteration of the loop which runs for however long the word is
printf("%c", cipherText[i]);
}
printf("\n");
// If input is not a positive integer then it will fail
if (key < 1)
{
printf("Usage: ./caesar key\n");
}
}
else
{
// If user inputted too many or no inputs then it will fail
printf("Usage: ./caesar key\n");
}
return 0;
}
string cipher(string word, int key)
{
// Find the length of the word in order to set the size of the array
// This is so that both strings, the word inputted and the word to return are the same to add to
int n = strlen(word);
string cipherText[n];
// Loop through the input word
for (int i = 0; i < n; i++)
{
// If char[i] is a letter then copy that letter into ciphertext
if (isalpha(word[i]))
{
cipherText[i] =& word[i];
}
else
{
cipherText[i] =& word[i];
}
}
// Return the array which, for example the input word is nobody
// Return array[n, o, b, o, d, y]
return cipherText[0-n];
}
The issue is that you are attempting to copy the address of the "word" character array characters into the associated cipher text array element which will print out unknown characters (noted in the above comments).
// Loop through the input word
for (int i = 0; i < n; i++)
{
// If char[i] is a letter then copy that letter into ciphertext
if (isalpha(word[i]))
{
cipherText[i] = &word[i];
}
else
{
cipherText[i] = &word[i];
}
}
When I ran your program with the code like that, I indeed got a series of question marks.
#Una:~/C_Programs/Console/CypherCS50/bin/Release$ ./CypherCS50 12
plaintext: Hello
?????
I then revised it to perform a copy of character elements from "word" to "cipherText".
// Loop through the input word
for (int i = 0; i < n; i++)
{
// If char[i] is a letter then copy that letter into ciphertext
if (isalpha(word[i]))
{
cipherText[i] = word[i];
}
else
{
cipherText[i] = word[i];
}
}
Then, reran the program.
#Una:~/C_Programs/Console/CypherCS50/bin/Release$ ./CypherCS50 12
plaintext: Hello
Hello
Seeing that the same data came out, my guess is that you still need to work on the actual encryption bits. But, the crux of the issue was referencing the memory of the work array elements.
Give that a try.
This does not fix your OP issue, but addresses another issue and responds to our exchange in comments above. Here is a "skeleton" demonstrating how you might approach incrementally developing code for this task. The 'excessive' printf's serve to prove that things are proceeding as you want as the source code becomes more elaborate..
// Functions defined before use do not need to be prototyped
// do-nothing "skeleton" to be completed
string cipher(string word, int key)
{
printf( "In cipher with key %d and str '%s'\n", key, word ); // temp code confirmation
return word; // for now...
}
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return -1; // early termination
}
printf( "argv[1] = %s\n", argv[1] ); // prove functionality
int key = atoi(argv[1]);
printf( "key = %d\n", key ); // prove functionality
if (key <= 0)
{
printf("Key must be positive integer");
return -1; // early termination
}
string plainText = get_string("plaintext: ");
printf( "plain = %s\n", plainText ); // prove functionality
string cipherText = cipher(plainText, key);
printf( "cipher = %s\n", cipherText ); // prove functionality
return 0; // All done!
}

Not sure how to proceed with Caesar's algorithm, cannot print out the deciphered text

I'm currently trying to convert a plaintext to a ciphertext with a key.
Code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int atoi(string argv);
int main(int argc, string argv[])
{
int k = 0;
string key = argv[1];
if (argc == 2)
{
for (k = 0; k < strlen(key); k++)
{
if (!isdigit(key[k]))
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
else
{
printf("\nSucces!");
}
}
}
else
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
string plaintext = get_string("\nplaintext: ");
for (int i = 0; i < strlen(plaintext); i++)
{
char c = plaintext[i];
if (isalpha(c))
{
printf("%c", (c + key) % 26);
}
}
}
If the character in the string of plaintext is a letter from the alphabet, I want it to move key positions.
I need to apply the following formula: ci = (pi + k) % 26, but it doesn't work out for me. Can someone hint me what I'm doing wrong?
I get the following error:
error: invalid operands to binary expression ('string' (aka 'char *') and 'int')
This (c + key) is the binary expression that provokes the error. c is a char (which is technically an int). key is a string, as declared and initialized here string key = argv[1];. There is a function in stdlib.c library called atoi, so it is a questionable decision to declare your own. That library needs to be include'd to use the atoi function. That is the function that can be used to convert key to an integer.

How Do I Rotate A Char Alphabetically By Integer X?

I've been trying different solutions but am not sure where to look for the solution.
I prompt the user for "plaintext" and whatever their input is, the chars in their input need to be rotated alphabetically by a number (aka the key) which they provide.
For example: plaintext: HELLO would spit out ciphertext: IFMMP if the key were 1.
Assuming the key will always be a number, here is what my code looks like which attempts to rotate each char by Key: 1. I'm a real noob so please break it down is possible.
{
string s = get_string("plaintext: ");
printf("ciphertext: %s\n", s + 1);
}
The remaining code (which includes identifying and filtering out the key is:
#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: ");
if(s)
{
printf("ciphertext: %s\n", s + 1);
free(s);
}
}
else
{
printf("Usage: ./caesar key\n");
}
}
else
{
printf("Usage: ./caesar key\n");
}
}
Any assistance would be appreciated.
I do not have cs50, so cannot test everything in your code, but addressing your primary question: How Do I Rotate A Char Alphabetically By Integer X, the code section you have identified as where the rotation occurs:
if (strlen(argv[1]) == counter)
{
string s = get_string("plaintext: ");
printf("ciphertext: %s\n", s + 1);
}
But this section doesn't really rotate the text. It rather attempts to print the string obtained from the call to get_string after adding 1 to it. First, this is not modifying s at all. Second, s + 1 is not a legal operation. To rotate s, additional code is needed between those two lines.
If I understand get_string(), it is essentially a combination of printf(), fgets(,,stdin) and strdup().
So after the call to that function you are left with s which will be populated with whatever the user typed into stdin. For illustration, let
say s contains "what the user typed". How that string is represented in memory, including the NULL terminator, can be depicted as follows:
|w|h|a|t| |t|h|e| |u|s|e|r| |t|y|p|e|d|0|
To rotate this by X, as you have stated, each character needs to be modified by adding the value X, in this case 1
Showing ASCII equivalent values:
|119|104|97|116|32|116|104|101|32|117|115|101|114|32|116|121|112|101|100|0|
The rotated string then would be each value + 1:
For the general case, assume rotation value is read in from command line argument and placed into int n = 0;
earlier in code:
if(isdigit(argv[2][0]))
{
n = atoi(argv[2])
}
The rotation: (the following is complete. It may need a little debugging around the corner case [where values wrap around the end of ASCII values when added to n.] Lines are commented indicating where I used ANSI C instead of CS50. [I do not have CS50])
int main(int argc, char *argv[])//note: command line requires a numeric
// argument be used: prog.exe 12 (for n == 12)
{
//char s[80];//used to test (I do not have cs50)
char *rotated = NULL;
int i;
if(argc != 2)
{
printf("Usage: prog.exe <positive int value.>\nExiting\n");
return 0;
}
if(isdigit(argv[1][0]) == 0)
{
printf("Usage: prog.exe <positive int value.>\nExiting\n");
return 0;
}
int n = atoi(argv[1]);
string s = get_string("plaintext: ");
// printf("Enter text to be rotated\n");//used to test (I do not have cs50)
// scanf("%[^\n]", s);
// string rotated = strdup(s); //preserve s by manipulating identical string
rotated = StrDup(s);
if(rotated)
{ //advance value by n
int origVal = 0;
int len = strlen(rotated);
for(i=0;i<len;i++)
{
origVal = rotated[i];
if((rotated[i] + n) <= 127)
{
rotated[i] += n;
}
else
{
rotated[i] = abs(127 - (origVal + n));
if(rotated[i] == 0) rotated[i] += 23; //skip to printable characters (http://facweb.cs.depaul.edu/sjost/it212/documents/ascii-pr.htm)
//if(rotated[i] == 0) rotated[i] += 1; //skip only ASCII NULL value
}
}
rotated[i] = 0; //adding NULL to end of string
printf("ciphertext: %s\n", rotated);
free(rotated);
}
return 0;
}
You can achieve this with typecasting:
char input = 'f';
char output = (char)((int)input + 1);
You should also handle edge cases, i.e. when the character value is near the end of the alphabet.

String concatenation with variables and pointers in 'C'

I am working on Caesar's cipher for an online course and I have a problem with the original value being in the final output, and I cannot seem to get rid of it. My suspicion is that it is due to
strcpy(str1, &final_val);
strcat(str2, str1);
being called in a wrong way, so when I run
make test && ./test 1
This provides my program with the argument 1 and provides the key to shift the letters and encode the message. I expect to see
plaintext: asd
ciphertext: bcd
Instead I get
plaintext: asd
ciphertext: bacbdc
If you want to try out the code, you will need to do it inside of this sanbox, because it has the required CS50 library.
Code
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
bool input_valid(int count, string arg);
bool in_alphabet(int count, string arg);
int main(int argc, string argv[]) {
int key;
int ascii_val;
char final_val;
string string;
char str1[80];
char str2[80];
// check input again if validation fails
if (!input_valid(argc, argv[1])) {
printf("Invalid input!\nUSAGE: ./caesar key\n");
return 1;
}
string = get_string("plaintext: ");
// get integer from string input
key = strtol(argv[1], NULL, 10);
for (int i = 0; i < strlen(string); i++) {
ascii_val = (int)string[i];
bool valid_lower_case = (ascii_val + key) >= 97 && (ascii_val + key) <= 122;
bool valid_upper_case = (ascii_val + key) >= 65 && (ascii_val + key) <= 90;
// check if value is a letter
if (isalpha(string[i])) {
// check if value is in the valid alphabet range
if (valid_lower_case || valid_upper_case) {
final_val = ascii_val + key;
} else {
// for lowercase: wrap around if the letter passes 'z'
final_val = 97 + (key - (122 - (ascii_val - 1)));
}
} else {
final_val = ascii_val;
}
strcpy(str1, &final_val);
strcat(str2, str1);
}
for (int i = 0; i < 5; i++) {
printf("%i\n", str2[i]);
}
printf("ciphertext: %s\n", str2);
}
bool input_valid(int count, string arg) {
// input has more args than just the file name
// input is an integer
return count > 1 && isdigit(arg[0]);
}
strcpy(str1, &final_val); is undefined behavior. strcpy expects both parameters to be pointers to null-terminated strings. However, since arrays decay to pointers when passed to functions, strcpy doesn't know the difference between a pointer to an array of characters and the address of a single char variable.
It will try to copy memory beginning at &final_val into str1, only stopping when it encounters a null terminator elsewhere in your process' memory, if there is one. To copy a single character to a string, just use str[x] = ch and str[x + 1] = '\0'.
Replaced strcopy() and strcat(). Got it to work by appending to str2 like this:
str2[i] = final_val;

Shift problem of cipher with Vigenere in C

I am extremely new to programming and I'm having some difficulties with Vigenere in C from the edX course CS50. I have broken the problem down into uppercase letters and lowercase letters and I am only trying to solve the uppercase letter problem right now. I am using the word 'panda' as my key and 'ILIKEYOU' as the plaintext. When I run the program, the first letter corresponds to the letter I'd expect it to be (23=X). After that, the program just seems to spit out random numbers for the remaining 7 letters. I haven't converted back to ASCII since I'm having so many problems with my code. Any ideas what is going on? Thank you all so much for the help :)
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
// Print error message if the user imput is executed without any
command-line arguments or with more than one command-line argument
if (argc != 2)
{
printf("Usage: ./vigenere k\n");
return 1;
}
// Access key
string key = argv[1];
// Convert the array from a string to an int
int letter;
letter = atoi(argv[1]);
// Print error message if the user imput is one command-line argument
and contains *any* non-alphabetical character(s)
for (int c = 0; c < strlen(key); c++)
{
if (!isalpha (key[c]))
{
printf("Usage: ./vigenere k\n");
return 1;
}
}
// Prompt the user for a string of plaintext
string p;
p = get_string("plaintext:");
//Print ciphertext
printf("ciphertext: ");
// Accessing each character in the plaintext
for (int i = 0, n = strlen(p); i < n; i++)
{
// Shift letters only in the plaintext
if (isalpha(p[i]))
{
// Convert plaintext and key to ASCII capital letters to
alphabetical index
// Encipher capital letters in plaintext and print
int c = 0;
if (isupper(p[i]))
{
printf("%i\n", ((p[i] - 65) + (toupper(key[c]) - 65)) % 26);
}
}
}
Needs few modifications -
int index_key = 0;
int shift= 0;
int key_len = strlen(key);
for (int i = 0, n = strlen(p); i < n; i++)
{
// Shift letters only in the plaintext
if (isalpha(p[i]))
{
// Convert plaintext and key to ASCII capital letters to
//alphabetical index
// Encipher capital letters in plaintext and print
if (isupper(p[i]))
{
shift = ((p[i] - 'A') + (toupper(key[index_key % key_len]) - 'A')) % 26;
index_key++;
printf("%c", p[i] + shift);
}
}
}

Resources