cs50 vigenere - loops incorrectly - cs50

Apologies if the answer to this is incredibly simple. I just can't work it out.
I've been working on the CS50 Vigenere problem and I think I'm almost there. However the program loops in a way that I don't expect and I'm not sure why. Once it has printed the first ciphered character of the plaintext, it loops back to move to the next character in the key but misses out the part where it needs to move to the next character of the plain text. At least I think that is what is happening.
Here is my code. Any help would be greatly appreciated.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv [])
{
int a;
int ciphertext;
int k;
string plain;
int cipher;
// check user has input a valid number of arguments
if (argc < 2 || argc > 2)
{
printf("please input a valid number of arguments\n");
return 1;
}
// check user has input a valid key and prompt for plaintext
char * key = argv [1];
for (a = 0; a < strlen(key); a++)
if (!isalpha(key[a]))
{
printf("Please input a valid key. Key must be alphabetical");
return 1;
}
{
if (a == strlen(key))
{
plain = get_string("Plaintext: ");
}
{
printf("ciphertext: ");
}
}
//read plaintext and keep track
{
for (int i = 0, n = strlen(plain); i < n; i++)
{
//read key and keep track
if (isalpha(plain[i]))
{
for (int j = 0, p = strlen(key); j < p; j++)
//convert key to numerical
{
if (isupper(key[j]) > 'A')
{
k = (key[j] - 65);
//calculate ciphertext and print (upper case)
{
printf("%c", (plain[i] + (k % p) %26) +65);
}
}
else if (islower(key[j]) > 'a')
{
k = (key[j] - 97);
{
printf("%c", (plain[i] + (k % p) %26) +97);
}
}
else printf("%c", plain[i]);
}
}
}
{
printf("\n");
}
}
}

Related

CS50 Readability pset2 debugging

Trying to get through cs50, stuck on readability. Can't figure out what is wrong with my code! When I plug in any length of text it is reading less than grade 1. I know it's got to be something with the counting of the letters, words, sentences but I just can't figure out where I am going wrong. Was hoping for some advice. Thank you!
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
//functions
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
//Prompt user for text
string text = get_string("Text: ");
//variables
int letters = 0;
int words = 1;
int sentences = 0;
//calculate grade level
float L = 100 * ((float)letters / (float)words);
float S = 100 * ((float)sentences / (float)words);
int index = round(0.0588 * L - 0.296 * S - 15.8);
{
if (index > 16)
{
printf("Grade 16+\n");
}
else if (index < 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade %i\n", index);
}
}
}
//Count number of letters
int letters = 0;
int count_letters(string text)
{
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
letters++;
}
}
return letters;
}
//Count number of words
int words = 1;
int count_words(string text)
{
for (int i = 0; i < strlen(text); i++)
{
if (isspace(text[i]))
{
words++;
}
}
return words;
}
//Count number of sentences
int sentences = 0;
int count_sentences(string text)
{
for (int i = 0; i < strlen(text); i++)
{
if ((text[i] == '!') || (text[i] == '?') || (text[i] == '.'))
{
sentences++;
}
}
return sentences;
}

cs50 caeser, I don't understand check50 fail

Below is my code and it is working for 10/11 checks, but shows the error message for this one:
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
Cause
expected "ciphertext: ia...", not "ciphertext: ia..."
Log
running ./caesar 12...
sending input world, say hello!...
checking for output "ciphertext: iadxp, emk tqxxa!\n"...
Expected Output:
ciphertext: iadxp, emk tqxxa!
Actual Output:
ciphertext: iadxp, emk tqxxa!
I have no idea why as they look identical to me, what obvious error am I missing here??
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(int key, string word[])
{
//Ensure only a key of 2 is accepted
if (key != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
//Ensure the second input has has no letters.
for (int i = 0, n = strlen(word[1]); i < n;)
if (isalpha(word[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
//Ensure each character of the input is a digit
else if (isdigit(word[1][i]))
{
i++;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
string text = get_string("Plain text: ");
{
printf("ciphertext: ");
}
{
//check each character is an alphabetical one
for (int b = 0, n = strlen(text); b < n; b++;)
{
int k = 0;
int cipher = 0;
if(!isalpha(text[b]))
{
printf("%c", text[b]);
}
else if (isalpha(text[b]))
{
//Calculate the cipher code for a lower-case letter
if (islower(text [b]))
{
int t = text [b] - 97;
k = (t + atoi(word[1])) % (26);
cipher = k + 97;
//Calculate the cipher code for an upper-case letter
}
else if (isupper(text[b]))
{
int t = text [b] - 65;
k = (t + atoi(word[1])) % (26);
cipher = k + 65;
}
}
//print cipher text.
{
printf("%c", cipher);
}
}
printf("\n");
}
}
First, to see what check50 sees, try this on the failing input.
./caeser 12 | cat -v
cat -v displays unprintable characters. The unprintable characters are coming from here printf("%c", cipher);. That line executes whether the input (text[b]) is alpha or not. That line needs to move inside the else if (isalpha(text[b])) block.

Beginners sincerely ask for advice about string

What the class describes is about "reversing a string", which is correct and usable from the Leetcode website. Today, I want to present "reversing a string" by inputting a value by myself (such as the int main() part below), but I still can't execute it after thinking for a long time. Beginners sincerely ask for advice, maybe you can also attach your writing so that I can learn, thank you.
#include <iostream>
#include <string>
using namespace std;
class Solution
{
public:
string reverseWords(string s)
{
if (s.size() == 0)
{
return s;
}
int front = 0, back = 0;
for (int i = 0; i < s.size() - 1; i++)
{
if (s[i] != ' ')
{
back++;
}
else
{
reverse(s.begin() + front, s.begin() + back);
front = back + 1;
back = front;
}
}
back++;
reverse(s.begin() + front, s.begin() + back);
return s;
}
};
int main()
{
Solution word01;
string s1= "Hello caterpillar";
word01 s1;
cout << s1.reverseWords();
}
Your code is pretty good, however we just want to reverse the words not the chars, for that we can use a while loop.
Similarly using two pointers, this'd pass just fine:
// The following block might trivially improve the exec time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <string>
#include <algorithm>
static const struct Solution {
using ValueType = std::uint_fast16_t;
std::string reverseWords(std::string s) {
std::reverse(std::begin(s), std::end(s));
ValueType len = std::size(s);
ValueType index = 0;
for (auto left = 0; left < len; ++left) {
if (s[left] != ' ') {
if (index) {
s[index++] = ' ';
}
ValueType right = left;
while (right < len && s[right] != ' ') {
s[index++] = s[right++];
}
std::reverse(std::begin(s) + index - (right - left), std::begin(s) + index);
left = right;
}
}
s.erase(std::begin(s) + index, std::end(s));
return s;
}
};
Here is LeetCode's solution with comments:
class Solution {
public:
string reverseWords(string s) {
// reverse the whole string
reverse(s.begin(), s.end());
int n = s.size();
int idx = 0;
for (int start = 0; start < n; ++start) {
if (s[start] != ' ') {
// go to the beginning of the word
if (idx != 0) s[idx++] = ' ';
// go to the end of the word
int end = start;
while (end < n && s[end] != ' ') s[idx++] = s[end++];
// reverse the word
reverse(s.begin() + idx - (end - start), s.begin() + idx);
// move to the next word
start = end;
}
}
s.erase(s.begin() + idx, s.end());
return s;
}
};
References
For additional details, please see the Discussion Board where you can find plenty of well-explained accepted solutions with a variety of languages including low-complexity algorithms and asymptotic runtime/memory analysis1, 2.

Why am I only getting 40% on Caesar?

I'm enrolled to CS50 online and I worked my way through Caesar as well as Vignere in PSet2. I got a score of 99% for Vignere but only 40% on Caesar, which is really strange as I worked off of Caesar for Vigenere.. If anyone could scan through and let me know where my code for Caesar lacks, I would really appreciate it!
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
//user to input number key unprompted
int main(int argc, string argv[])
{
//iterate to make sure only one argument is inputted
if (argc != 2)
{
printf("Usage: ./caesar key \n");
}
else
{ //iterate to make sure the string only has numeric digits
for (int j = 0; j <= (strlen(argv[1])); j++)
{
if (((argv[1][j] >= 'a') || (argv[1][j] >= 'A')) && ((argv[1][j] <= 'z') || (argv[1][j] <= 'Z')))
{
printf("Usage: ./caesar key \n");
return 1;
}
}
//converting digit string to an integer called key
int key = atoi(argv[1]);
if (key < 0) //making sure the key is positive
{
printf("Usage: ./caesar key \n");
return 1;
}
//after validity has been checked
else
{
//prompt user to input plaintext
string plain = get_string("plaintext: ");
int len_plain = strlen(plain);
//convert plaintext to ciphertext using inputted key
string cipher = plain;
for (int x = 0; x < len_plain; x++)
{
if (plain[x] >= 'a' && plain[x] <= 'z')
{
cipher[x] = ((plain[x] + key)%122);
if (cipher[x]<97)
{
cipher[x] = cipher[x] + 96;
}
}
else if (plain[x] >= 'A' && plain[x] <= 'Z')
{
cipher[x] = ((plain[x] + key)%90);
if (cipher[x] < 65)
{
cipher[x] = cipher[x] + 64;
}
}
else
{
cipher[x] = plain[x];
}
}
printf("%s\n", cipher);
}
}
}
From the spec (emphasis added):
... after validating the key, we prompt the user for a string (using
"plaintext: " for the prompt) and then shift all of its characters by
1, printing out "ciphertext: " followed by the result and a newline.
The program does not print "ciphertext: " in the result.

Weird shapes as output( Strings)- C language

So i have a problem. I have to separate the first name, last name and hostname of email.
For example:
zephyr.extreme#gmail.com>> Input
Output=
First name= Zephyr
Last name= extreme
Host Name= gmail.com
I am not getting the desired result. I am getting some weird shapes as output.
Code:
#include <stdio.h>
int main()
{
char email[40], first[20],last[20],host[30];
printf("Enter the email= ");
gets(email);
int i;
while(email[i]!='\0')
{
while(email[i]!='.')
{
first[i]=email[i];
i++;
}
while(email[i]!='#')
{
last[i]=email[i];
i++;
}
while(email[i]!='\0')
{
host[i]=email[i];
i++;
}
}
puts(first);
puts(last);
puts(host);
}
Assuming the format will always be first.last#host..., use this code:
#include <stdio.h>
#include <string.h>
int main()
{
char email[40], first[20],last[20],host[30],name[40];
int firstDot,atSymbol;
int i;
int length;
char *token;
printf("Enter the email= ");
gets(email);
length = strlen(email);
for(i=0;i<length;i++){
if(email[i]=='.')
{
firstDot = i;
}
else if(email[i]=='#')
{
atSymbol = i;
}
}
strncpy(name,email,atSymbol);
name[atSymbol]= '\0';
token = strtok(name, ".");
/* walk through other tokens */
while( token != NULL )
{
printf( "%s\n", token );
token = strtok(NULL, ".");
}
strncpy(host,email+atSymbol,length-atSymbol);
host[length-atSymbol] = '\0';
puts(host);
}
So i updated the code, now the only problem is the last output.
After host name= gmail.com prints, but then some extra shapes are also printing. These are smile face and some weird symbols.
Code:
#include <stdio.h>
int main()
{
char email[40], first[20],last[20],host[30];
printf("Enter the email= ");
gets(email);
int i=0,j;
while(email[i]!='.')
{
first[i]=email[i];
i++;
}
i=0;
while(email[i]!='#')
{
last[i]=email[i];
i++;
}
j=i;
i=0;
while(email[j]!='\0')
{
host[i]=email[j];
j++;
i++;
}
printf("First Name= ");
puts(first);
printf("Last name= ");
puts(last);
printf("Host name= ");
puts(host);
}
C strings (char pointers) should be null-terminated. This means your string needs a '\0' character at its end so that string manipulation functions such as puts or strlen know where they end, in constrast to other languages where the string's length is stored together with it. The "weird shapes" you are seeing are just random data stored after the end of the string being interpreted as characters. When you call puts it just keeps outputting bytes-as-characters until it randomly finds a byte with value '\0'
You can solve this by adding a '\0' character to the end of the string after each of the blocks where you write a string.
while(email[i]!='.')
{
first[i]=email[i];
i++;
}
email[i] = '\0'; //same thing as email[i] = 0; but using a char makes what
//you're doing clearer

Resources