Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently doing CS - PSET2, and I wrote the code below.
Everything is working, but now that I am re-reading it, I don't understand why.
The doubt is with line 11: if(isdigit(argv [1][i])).
Let's say I write "35", first char is a number, so loop is true and it starts --> I have to write a string (Line 16).
Now program will check the second char, "5", it is a number, true --> I have to write a string again theoretically.
But why not? Why don't I get double results for everything?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main (int argc , string argv [])
{
if (argc == 2 && isdigit(argv [1]))
{
int n = strlen(argv [1]);
for (int i=0; i<n; i++)
{
if(isdigit(argv [1][i]))
{
string plaintext = get_string("Plaintext: ");
int key = atoi(argv[1]);
int l = strlen(plaintext);
for(int p=0; p <l; p++)
if isupper(plaintext[p])
{
printf("%c", (((plaintext[p] - 'A') + key) % 26) + 'A');
}
else if islower(plaintext[p])
{
printf("%c", (((plaintext[p] - 'a') + key) % 26) + 'a');
}
else
{
printf("%c", plaintext[p]) ;
}
return 0;
}
else
{
printf("Nope\n");
return 1;
}
}
}
else
{
printf("Nope\n");
return 1;
}
}
You have a return 0; statement inside the if that checks for a digit. This will cause it return before it can loop again. Just get rid of that return.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
Sorry, I know this has been asked before but I have read all the answers and nothing works! Please help.
When I check acutal and expected outputs, they match, but in check50 It gives an error message.
Here is the link for check50 results: https://submit.cs50.io/check50/6d939efb8a55e3fadec1c60952311f6198cd0eb0
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char crypt(int k,char w)
{
if ('a'<= w && w <='z')
{
return (((w-'a')+ k)%26+'a');
}
else if ('A' <= w && w <= 'Z')
{
return (((w -'A') + k) % 26 + 'A');
}
else
{
return (w);
}
}
int main(int argc, string argv[])
{
//input check
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
int lngg = strlen(argv[1]);
for (int i = 0; i < lngg; i++)
{
if (isdigit(argv[1][i]) == 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1])%26;
//input
string plain = get_string("plaintext: ");
//crpypt starts
printf("ciphertext: ");
int lng = strlen(plain)+1;
for (int a = 0; a < lng ;a++)
{
printf("%c", crypt(key, plain[a]));
}
printf("\n");
}
Hi, When I look expected and actual outputs, everything seems perfect but it errors
The problem is that you are also printing the terminating null character to standard output using printf. This character is not printable, so you cannot see it, but check50 does detect it and therefore reports that your program failed.
The loop
for(int a=0; a<lng ;a++)
{
printf("%c",crypt(key,plain[a]));
}
will run for strlen(plain)+1 iterations, because that is the value to which you set the variable lng.
You should instead set the value of lng to strlen(plain), in order to prevent the terminating null character from being printed.
#w is correct.
Also, code can avoid 2 passes down the string and do only 1.
// int lng = strlen(plain)+1;
// for (int a = 0; a < lng ;a++)
for (int a = 0; plain[a] != '\0'; a++)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This problem aims to apply Caesar Cipher. The goal is to encrypt a user defined message. The problem requires us to get the key using the command prompt. After getting the message and the key we must encrypt each character by moving by the key. e.g. if the key is 1 then A - B and b - c and z - a, if the key is 2 then a - c, D - F etc... I have made a very solid program and it seems very correct yet when I use the check50 to check my code it appears so many errors yet the outputs are the same... I just don't get what's wrong with my code..
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
for (int i = 0; argv[1][i] != '\0'; i++)
{
if (isalpha(argv[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]);
string ptext;
ptext = get_string("plaintext: ");
printf("ciphertext: ");
int n = strlen(ptext);
for (int i = 0; i <= n; i++)
{
if (isupper(ptext[i]))
{
printf("%c", (((ptext[i] + key) - 65) % 26) + 65);
}
else if (islower(ptext[i]))
{
printf("%c", (((ptext[i] + key) - 97) % 26) + 97);
}
else
{
printf("%c", ptext[i]);
}
}
printf("\n");
return 0;
}
link to check50
https://submit.cs50.io/check50/f7714d6b10c0b2e9fd1c1f01f4209195d8dc5163
You're outputting the null terminator character.
int n = strlen(ptext);
for (int i = 0; i <= n; i++)
You should only loop to i < n.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am having this issue, where I use a command-line interface.
I am trying to store the input in an array of strings. But When I run my code
I get a segmentation fault?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int num, char **str) {
int i;
char owner[20];
char *keys[5];
int j = 0;
for (i = 1; i < num; i++) {
if (i == 1) {
strcpy(owner, str[i]);
printf("%s", owner);
}
else {
keys[i] = malloc(10 * sizeof(char));
strcpy(keys[j], str[i]);
printf("%s", keys[j]);
j++;
}
}
}
In the else statement you should write
else {
keys[j] = malloc( strlen( str[i] ) + 1 );
strcpy(keys[j], str[i]);
printf("%s", keys[j]);
j++;
}
Also you should provide that the array owner is large enough to store the string str[1].
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to print 1-1000 without 998. This code prints up to 997 then it stops. What is the problem with this code?
#include<stdio.h>
int main() {
int n =1;
while(1){
if(n==998){
continue;
}
printf("%d\n",n);
n++;
if(n>1000){
break;
}
}
return 0;
}
The reason is that we never increment n once we reached the 998
One solution would be to move n++; above the if (n == 998) continue; or vice versa, i.e.:
#include <stdio.h>
int main()
{
int n = 0;
while (1) {
n++;
if (n == 998)
continue;
if (n > 1000)
break;
printf("%d\n", n);
}
return 0;
}
Your solution stuck at 998 because you always execute continue to enter the next iteration without incrementing your value. Here is a much more simple, compact form:
#include <stdio.h>
int main() {
for (int i = 1; i <= 1000; i++) {
if (i == 998) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Problem is with your condition:
if(n==998){
continue;
}
It won't increment the variable "n".
One way to fix it is the following code:
if(n==998){
n++;
continue;
}
Once n is 998, you jump to the next iteration of the loop without incrementing n. This results in an infinite loop where nothing is being output.
Rather than using continue when n is 998, instead you can print if it is not 998:
#include<stdio.h>
int main() {
int n =1;
while(1){
if(n!=998){
printf("%d\n",n);
}
n++;
if(n>1000){
break;
}
}
return 0;
}
This can be simplified further by using a for loop rather than an infinite while loop with the exit condition inside:
#include<stdio.h>
int main() {
int n;
for (n=1; n<=1000; n++) {
if(n!=998){
printf("%d\n",n);
}
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to implement the Caesar cipher but I'm not getting the expected output. What's wrong with the code?
Key: 3
Input: Hello
Output I'm getting: KNUUX
Expected Output: KHOOR
Code:
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
if(argc!=2)
{
return 1;
}
int k, i;
k = atoi(argv[1]);
printf("Enter the String to Encrypt: ");
string s=GetString();
for(i=0; i<strlen(s); i++)
{
if('A'>=s[i]<='Z')
{
s[i]=((s[i] - 'A' + k)%26) +'A';
}
else if('a'>=s[i]<='z')
{
s[i]=((s[i] - 'a' + k)%26) +'a';
}
}
printf("The Encrypted Text is %s\n",s);
}
if('A'>=s[i]<='Z')
Is certainly not doing what you seem to be expecting.
You probably want:
if ( (s[i] >= 'A') && (s[i] <= 'Z') )
for(i=0; s[i]; ++i)
if(isalpha(s[i]))
s[i]=(toupper(s[i]) - 'A' + k)%26 +'A';